PROJECT Nº 1: Interactive robot

Learn to build and program an interactive robot using the components of the Maker Control Kit and Maker Kit 3.

Use the servomotor, the distance sensor, and the red and yellow LEDs to build the robot system. When the distance sensor detects an object or movement in front of it, the LEDs on the robot will blink alternating, and it will move its arm to wave. Create your first interactive robot!

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE: 60 min.

MATERIALS:

  • 1 Servomotor
  • 1 Ultrasonic distance sensor
  • 1 Red LED
  • 1 Yellow LED
  • 1 4in1 Build&Code board
  • 1 USB – Micro USB cable
  • Computer
  • Material to create the robot
  • Adhesive or tape

What is an ultrasonic distance sensor?

The ultrasonic sensor is a device used to measure distance. It operates by emitting a high-frequency sound pulse, not audible to the human ear. This pulse bounces off of nearby objects and is reflected toward the sensor, which has a microphone capable of picking up this frequency.

By measuring the time between pulses and knowing the speed of sound, we can estimate the distance of an object whose surface was hit by the ultrasonic pulse.

BUILDING THE STRUCTURE:

To build the robot’s body, the arm, and support, you will use sheets of poster board.

In order to build the structure, download the interactive robot stencil and the quick assembly guide. Follow the steps provided.

To fasten the electronic components to the structure, use tape or adhesive. This is the case for the LEDs and the joining piece of the arm with the servomotor.

Once the robot structure is ready, connect all of the electronic components and the USB cable following the indications in the section on connections found below.

CONNECTIONS:

  1. Connect the red LED to digital port 4 on the 4in1 Build&Code board.
  2. Connect the yellow LED to digital port 5 on the 4in1 Build&Code board.
  3. Connect the servomotor to digital port 9 on the 4in1 Build&Code board.
  4. Connect the ultrasonic distance sensor to digital ports 12 and 13 on the 4in1 Build&Code board.

Look at the colors of the cables and the colors of the terminals on the 4in1 Build&Code board to guide you. Each cable should be connected to its color:

PROGRAMMING CODE:

The program consists of reading the distance from the ultrasonic distance sensor. If the distance is less than 30 cm and greater than 1 cm, it will repeat the process of moving the servomotor from 140º to 60º and turning the red and yellow LEDs on and off at intervals of 0.3 seconds twice.

If the distance is greater than 30 cm, it will keep the position of the servomotor at 140º and the LEDs will stay on without blinking. As long as the system has a power supply, this sequence will keep repeating.

You can do this activity by using Arduino and Bitbloq software, as well as other compatible block programming software. Below you will find the programming code needed for each software.

Arduino Code

  1. Download the Arduino software and go through the installation process.
  2. Open the Arduino program, and once in, copy the following program:
    #include <Servo.h>
    Servo motor1; // Declare servomotor
    int TrigPin = 13;  // ULTRASONIC SENSOR PORTS
    int EchoPin = 12;  
    float SSound = 0.0343;  // SPEED OF SOUND IN cm/us
    long Lengh, Distance ;  // VARIABLES TO CALCULATE THE DISTANCE IN cm
    int RedLED = 4, YellowLED = 5;
    
    void setup() {
      // Put your setup code here, to run once:
      motor1.attach (9); // DIGITAL PORT PWM 9 WHERE SERVO 1 IS CONNECTED
      pinMode(TrigPin, OUTPUT); // DIGITAL PORTS 12 Y 13 WHERE THE ULTRASONIC SENSOR IS CONNECTED  
      pinMode(EchoPin, INPUT);
      pinMode(RedLED, OUTPUT);
      pinMode(YellowLED, OUTPUT);
    }
    
    void loop() {
      // Put your main code here, to run repeatedly:
      DistanceCM(); // CALL THE FUNCTION TO CALCULATE THE DISTANCE
      if (( Distance < 30) && ( Distance > 1)) // IF THE DISTANCE IS BETWEEN 1 AND 30 cm
        {
          // WAVE TWO TIMES and BLINK THE LEDS
          for (int x=0; x<2 ; x++)
          {
            motor1.write (140); 
            digitalWrite(RedLED, HIGH);
            digitalWrite(YellowLED, LOW);
            delay (300);
            motor1.write (60); 
            digitalWrite(RedLED, LOW);
            digitalWrite(YellowLED, HIGH);
            delay (300);
          }
          
        }
        else
        {
          // INITIAL POSITION AND LEDS ON, SET
          motor1.write(140);  
          digitalWrite(RedLED, HIGH);
          digitalWrite(YellowLED, HIGH);
        }
    }
    
    void DistanceCM()// FUNCTION TO CALCULATE DISTANCE
    {
      // DISTANCE CALCULATION IN cm 
      digitalWrite(TrigPin, LOW);        // We check that the trigger is deactivated
      delayMicroseconds(4);              // To make sure that the trigger is LOW
      digitalWrite(TrigPin, HIGH);       // We activate the output pulse
      delayMicroseconds(14);             // We wait 10µs. The pulse is still active this time
      digitalWrite(TrigPin, LOW);        // We cut the pulse and wait for ECHO
      Lengh = pulseIn(EchoPin, HIGH) ; //pulseIn measures the time that elapses from when the declared pin (echoPin) changes from low status to high (from 0 to 1)
      Distance = SSound* Lengh / 2;  // CALCULATION OF THE DISTANCE
    }
    
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

Code for compatible block programming software

  1. Download the software and go through the installation process.
  2. Open the program, and once in, copy the following code:
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

BitBloq Code

  1. Get the BitBloq software.
  2. Open the BitBloq program, and once in, copy the following code:
    • Hardware
    • Software
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

RESULT OF THE EXERCISE:

The robot waves and its LEDs blink alternatingly when it detects a movement or an object less than 30 cm away. If it does not detect an object or movement less than 30 cm away, it stays at its resting position. You have created your first interactive robot!

1 0

0