In this workshop you build and program a small autonomous robot using the following components:
- 1 x mini breadboard
- 1 x Arduino Nano
- 1 x USB cable
- 1 x green LED (220 ohm resistor listed below)
- 1 x SN754410NE IC
- 2 x yellow motors (with wires soldered on the terminals)
- 1 x TCRT5000 sensor
- 1 x 4-terminal connector strip
- 1 x 10kOhm resistor
- 1 x 220 ohm resistor with long legs
- 1 x 220 ohm resistor with short legs
- 1 x 220uF capacitor
- 1 x 4AA battery holder
- 4 x AA batteries
- 3 x 4cm red
- 2 x 4cm black
- 1 x 8cm green
- 4 x 8cm yellow
- 1 x 20cm red
- 1 x 20cm black
- 1 x 20cm blue
- Laser-cut acrylic chassis and wheels
The following additional materials will be provided to construct the bodywork of the robot.
- 3mm foam board
- Foil turkey tray
- Ranger bands for tyres (these are cut from bicycle inner tubes)
Part 1: Flashing a light
The “breadboard” is the rectangular white plastic board we use to build the circuit. Each short row of five holes is a single electrical connection. Two wires inserted into the same row become connected electrically. Each row is marked with a number and each column is marked with a letter, so that each hole in the main section of the board can be uniquely identified using a letter and number. The instructions below use these letters and numbers to identify the exact position of each component.
Before beginning, make sure your breadboard is the same way around as the picture below.
The first component in the circuit is the Arduino Nano, which is a simple computer in a tiny package. This is the brain of the robot. You control the robot by writing programs that run on the Arduino.
Make sure your Arduino is the right way around, with the mini USB socket at the end of the breadboard (row 1). The pin marked D12 should be in breadboard hole H1. Some Arduinos can be very difficult to insert into the breadboard, so if you’re having problems just ask for help because you might have just received a particularly tricky one.
On each side of the breadboard there are two long rows of holes which are connected along the full length of the board. These rails are used to distribute the supply voltage to different parts of the circuit. The blue line marks the negative rail (0V); the red line marks the positive rail (6V in this circuit). The Arduino draws its power from these rails.
- Connect a short black wire between A14 and the negative (blue) rail.
- Connect a short red wire between A15 and the positive (red) rail.
The first thing we’ll control with the Arduino is a green light-emitting diode (LED). To do this, we’ll turn the Arduino pin marked D2 into a digital output which means that the program running on the Arduino can set it high (5V) or low (0V). When the pin is set high, a small electrical current flows through the green wire, through the green LED, and finally through the resistor to ground (the negative rail).
- Insert the green wire between I11 and A18.
- Insert the green LED between E18 and E19. The LED is a one-way valve for electricity, so it must be the right way around. Inside the green plastic bead, if you look carefully you’ll see that each leg is connected to a kind of a flat plate. As shown in the image below, the leg connected to the larger plate should be in E19.
- Insert the 220 ohm resistor (colour code RED RED BROWN GOLD ) with the short legs between B19 and the negative (blue) rail.
We’re ready to run a program on the Arduino to flash the green LED on and off.
- Double-click the Arduino icon on the desktop to launch the Arduino IDE (integrated development environment).
- Delete the example code that appears by default in the editor.
- Copy and paste the code below into the editor.
// // RoboSlam example 1: Blink LED // // The setup routine runs once when the power is switched on. void setup() { // Digital output for LED on pin D2 pinMode(2, OUTPUT); } // The loop routine runs over and over until the power is switch off. void loop() { digitalWrite(2, HIGH); // LED on delay(1000); // 1000ms delay digitalWrite(2, LOW); // LED off delay(1000); // 1000ms delay }
Before running the program on the Arduino Nano, you need to select the correct version of Arduino.
- Under the “Tools” menu, enter the “Board” sub-menu and select “Arduino Nano w/ ATmega328” as shown in the image below. If that item is not listed, select “Arduino Nano” instead.
- Under the “Tools” menu, enter the “Serial Port” sub-menu and select whatever device is listed there.
- If the “Serial Port” sub-menu is not accessible, please ask a facilitator to check your machine because the Arduino drivers may not be set up correctly.
To download and run the program on the Arduino, click the right-facing arrow button on the toolbar of the Arduino IDE:
At this point you should hopefully see the green LED flashing on and off slowly. If it’s not and you can’t figure out why, please ask a facilitator to check what’s wrong.
Once the LED is flashing, try shortening the delays in the “loop” routine to speed up the flashing. Each time you modify the program you need to click the right-facing arrow button in the toolbar to update the code on the Arduino.
Part 2: Motor control
In Part 1, we learned how to set an Arduino pin high and low so that it switched a light on and off. Now let’s switch something more interesting on and off: motors.
The Arduino’s output pins can only supply a very small electric current. It’s enough to light an LED, but it’s much too small to drive the motors. We therefore use a driver chip to interface the Arduino with the motors.
- Place the SN754410NE chip in the breadboard as shown below. Pin 1 (at the end of the chip with the small semi-circular indentation) should be in E21.
- Insert a short red wire between G21 and D28.
- Insert a short red wire between A28 and the positive (red) rail.
- Insert a short black wire between A25 and the negative (blue) rail.
These red and black wires supply power to the SN754410NE chip which it uses to drive the motors. That power comes from the battery pack, which holds four 1.5V AA batteries providing a total supply voltage of 6V. When the motors switch on and off they disrupt the 6V supply voltage, but a capacitor connected between the rails helps to stabilise it a bit.
- Connect the battery pack to the rails as shown below. The switch shown on the red wire of the battery pack is actually integrated into the main body of the battery pack you’re using in today’s workshop.
- Connect the 220uF capacitor between the rails as shown below. A stripe on one side of the cylindrical body of the capacitor marks the minus leg, which should be connected to the negative (blue) rail. The other leg should be connected to the positive (red) rail.
Before connecting the motors to the circuit, we need to build the main chassis of the robot. This is like the physical skeleton of the robot that everything else will be attached to. The following video shows how to assemble the chassis and attach the motors and wheels.
The breadboard circuit can now be placed on top of the chassis and the motors can be connected to the four output pins of the SN754410NE chip as shown below.
- Connect the wires of one motor to C23 and B26.
- Connect the wires of the other motor to H23 and I26.
- Insert a yellow wire between I10 and D27.
- Insert a yellow wire between I9 and D22.
- Insert a yellow wire between I6 and G22.
- Insert a yellow wire between I5 and G27.
Each output pin on the SN754410NE chip is controlled by an input pin right next to it. We connect each of those four input pins to a digital output pin on the Arduino, which allows the Arduino to control the motors.
Now let’s program the Arduino to control the motors by switching those four new outputs on and off. Paste the following code into the Arduino IDE and press the right-facing arrow button on the toolbar to download and run it.
// // RoboSlam example: Zigzag driving // // The setup routine runs once when the power is switched on. void setup() { // Digital output for LED pinMode(2, OUTPUT); // Digital outputs for left motor pinMode(3, OUTPUT); pinMode(4, OUTPUT); // Digital outputs for right motor pinMode(7, OUTPUT); pinMode(8, OUTPUT); } // The loop routine runs over and over until the power is switched off. void loop() { // forward for 2000ms digitalWrite(2, LOW); // LED off digitalWrite(3, HIGH); // Left motor forward digitalWrite(4, LOW); digitalWrite(7, HIGH); // Right motor forward digitalWrite(8, LOW); delay(2000); // turn left for 1000ms digitalWrite(2, HIGH); // LED on digitalWrite(3, LOW); // Left motor reverse digitalWrite(4, HIGH); digitalWrite(7, HIGH); // Right motor forward digitalWrite(8, LOW); delay(1000); // forward for 2000ms digitalWrite(2, LOW); // LED off digitalWrite(3, HIGH); // Left motor forward digitalWrite(4, LOW); digitalWrite(7, HIGH); // Right motor forward digitalWrite(8, LOW); delay(2000); // turn right for 1000ms digitalWrite(2, HIGH); // LED on digitalWrite(3, HIGH); // Left motor forward digitalWrite(4, LOW); digitalWrite(7, LOW); // Right motor reverse digitalWrite(8, HIGH); delay(1000); }
[Note: Just in case you’re interested, here’s a slightly more elegant version of the same program which wraps up the motor control code in handy reusable functions.]
Part 3: Using a colour sensor to control the robot
In part 2 we made the robot move around, but it just moved in a fixed sequence without responding to anything in its environment. In this part, we add a colour sensor so that it can navigate using coloured markings on the ground.
The TCRT5000 infrared reflective sensor is the component in the small black plastic casing with four legs, one blue eye and one black eye. The blue eye is an infrared LED which emits a beam of infrared light when current flows through it. The black eye is a phototransistor which detects infrared light. When the sensor is close to a surface, some of the emitted light is reflected back and detected by the phototransistor, but the amount of light reflected back depends on the colour of the surface. This is how the robot senses the colour of the ground. We’re going to use the TCRT5000 to build a 3-wire colour sensing module.
- Lay out the parts shown in the image below. The three wires visible at the bottom of the image are the long red, blue and black wires.
- “TCRT5000” is written in tiny letters on one side of the colour sensor – make sure that side is facing downwards (towards the table).
- The two legs of the TCRT5000 that are now higher off the table (i.e. closer to the camera in the image below) should be carefully bent towards each other until they meet.
Insert the components and wires into the terminal block as shown in the images below and tighten each terminal with a screwdriver to hold them all in place.
Finally, connect the three wires of the colour sensor module to the circuit.
- Connect the red wire to the positive (red) rail.
- Connect the black wire to the negative (blue) rail.
- Connect the blue wire to B4, which feeds the sensor output into pin A0 of the Arduino.
The voltage on the blue wire of the sensor module depends on the colour it sees. When the sensor is pointing at a black surface the voltage is low and when it points at a white surface the voltage is high. This voltage is connected to pin A0, so it can be read by the program running on the Arduino and used to control what the motors do.
Re-program the Arduino using the following code:
// // RoboSlam example: Colour sensor control // // Declare an integer variable to store the colour readings int colour; // The setup routine runs once when the power is switched on. void setup() { // Digital output for LED pinMode(2, OUTPUT); // Digital outputs for left motor pinMode(3, OUTPUT); pinMode(4, OUTPUT); // Digital outputs for right motor pinMode(7, OUTPUT); pinMode(8, OUTPUT); // Open serial port to display colour readings on screen Serial.begin(9600); } // The loop routine runs over and over until the power is switched off. void loop() { // Read the voltage on pin A0 (the colour sensor voltage) colour = analogRead(A0); // Print the colour value via the serial port Serial.println(colour); // Choose motor action based on colour reading if (colour < 512) { // black detected digitalWrite(2, LOW); // LED off digitalWrite(3, HIGH); // Left motor forward digitalWrite(4, LOW); digitalWrite(7, HIGH); // Right motor forward digitalWrite(8, LOW); } else { // white detected digitalWrite(2, HIGH); // LED on digitalWrite(3, HIGH); // Left motor forward digitalWrite(4, LOW); digitalWrite(7, LOW); // Right motor reverse digitalWrite(8, HIGH); } }