Introduction: Autonomous Robot Racer: Scratch Mode
In this project, you'll learn how to make an autonomous wall-detecting robot. This robot uses an Arduino Nano and Scratch to avoid walls and race around the track. The Tompkins County Public Library uses this robot to hold weekly robot racing events. This is one of several autonomous robot racers developed at TCPL. More robot Instructables coming soon!
Made and developed by Joah Tang, Teen Service Library Assistant at TCPL.
Supplies
Laser Cut Files:
3D File:
Electronics and Other Parts:
- 9V Battery Case with Switch
- L298N Motor Driver
- DC Motors with Gearbox
- Breadboard
- Breadboard wires
- Arduino Nano
- IR Obstacle Avoidance Sensors
- M3 nuts and bolts
- Size #64 rubberbands
- Rechargeable 9v battery
- 12 x 12 plywood
- Electrical Tape
Tools:
- Wire stripper
- Plastic nippers
- Hot glue gun
- Phillips head screwdriver
- USB 2.0 cable
Step 1: Prepare Laser Cut and 3D Printed Parts
At TCPL we are privileged to have access to an Epilogue Laser and Prusa 3D printers. Please check your local Makerspace or Library for public-access laser cutters and 3D printers.
- Download the Laser Cut Files in the Supplies section from Tinkercad as an SVG file.
- Upload files to your favorite vector software. TCPL uses Inkscape. Prepare the files based on the requirements of the laser you are using.
- Laser cut the parts on a 12x12 1/8 inch plywood board.
- Once the parts are cut out hot-glue matching wheel pairs together.
- Stretch a rubber band over each wheel.
- Apply and spread hot glue along the rectangles at the back of the chassis. See the second photo for an example. This will keep the motors from sliding around and make then adjustable.
- Download the 3D Print File in the Supplies section from Tinkercad as an STL file.
- Import the STL in your favorite slicer. Make sure to set the infill to 15% and supports on.
- Once the print is complete clean off the supports.
Note: If you are unable to access a laser you can print the SVG files out and use a scroll or coping saw and drill to cut out the parts.
Step 2: Wire Motors
At TCPL we have developed a way to attach wires to the DC motors without the need for solder. This was done to streamline repairs during races. If you are comfortable using solder attach the wires that way.
- Take two M-to-M wires and cut them in half and strip each end without a pin.
- Twist the wire brush into a tight wrap and slide it into the DC motor input. See the second picture for an example.
- Once the brush is secure in the DC motor input lightly pull it down the side of the motor bell and glue it in place. Be careful not to get glue on the motor input of the shaft. See the third picture for an example.
- Repeat the process for the other DC motor input so there are two wires attached to it.
- Tightly wrap the electrical tape around the bell of the motor to cover the exposed wire brushes.
- Repeat this process for the second motor.
- Once both motors are ready slide a motor clip on each. You will see a round circle protruding from one side of the motor, the open side of the clip goes around this. See the 9th picture for an example.
- Slide a clip onto each motor.
Step 3: Assemble Chassis
- Slide motors onto the rear of the chassis. See the first picture for orientation.
- Using two screws and nuts secure the L293N motor driver to the center of the chassis. See the second image for orientation.
- Hot glue the 9v battery case to the chassis so the switch is adjacent to the motor driver.
Step 4: Power Distribution Circuit
- Take a red and black wire that has pins on both ends. Cut each in half. You will only use a half of each. You can save he other halves for backup.
- Take the red wire and strip the end without a pin.
- Take the black wire and strip the end without a pin.
- Take the black wire coming from the 9v battery pack and strip it. Twist the black wire with a pin and the one from the battery pack together.
- Input the exposed end of the joined black wires into the GND port on the L298N motor driver. Secure the GND port by tightening its screw.
- Take the red wire coming from the 9v battery pack and strip it. Twist the red wire with a pin and the one from the battery pack together.
- Input the exposed end of the joined red wires into the 12V port on the L298N motor driver. Secure the 12V port by tightening its screw.
- Press the Arduino Nano into the mini breadboard. Make sure the side with VIN pin label has three rows of inputs and the other side two. See the fourth picture for example.
- Input the black wire coming from the GND port on the L298N motor driver next to the Arduino Nano GND port on the breadboard. Note: any one of the Arduino Nano GND ports will work. In this Instructable I put the next to the one on the VIN side of the Arduino Nano.
- Input the red wire coming from the 12V port on the L298N motor driver next to the Arduino Nano VIN port on the breadboard.
Step 5: Add Sensors
The orientation of the robot for this step is the front end facing you. See second picture for example.
- Take one of the IR Object Avoidance sensors and attach the following breadboard wire to its pins.
- Attach a red wire with an input and pin to the VCC pin.
- Attach a black wire with an input and pin to the GND pin.
- Attach a green wire with an input and pin to the OUT pin.
- Take the remaining IR Object Avoidance sensors and attach the following breadboard wire to its pins.
- Attach an orange wire with an input and pin to the VCC pin.
- Attach a brown wire with an input and pin to the GND pin.
- Attach a yellow wire with an input and pin to the OUT pin.
- Using an m3 screw and nut secure the sensor with the green wire to the left side of the car front end. Make sure to angle the sensor out. You will see three lines cutout at the front of the car you can attach the sensor to any of these. See the second picture for example.
- Using an m3 screw and nut secure the sensor with the yellow wire to the right side of the car front end. Make sure to angle the sensor out. You will see three lines cutout at the front of the car you can attach the sensor to any of these. See the second picture for example.
- Insert the red and orange wire pins coming from the sensors into the breadboard next the 5V pin on the Arduino Nano. See picture 4 and 5 for examples.
- Note: you can wrap the wire around any of the cut out areas on the chassis to keep the wires tidy. This is not requires though. See the 3rd picture for an example.
- Insert the brown and black wire pins coming from the sensors into the breadboard next the GND pin on the Arduino Nano. See picture 4 and 5 for examples.
- Insert the yellow wire pin coming from the sensor into the breadboard next the D12 pin on the Arduino Nano. See the 6th picture for an example.
- Insert the green wire pin coming from the sensor into the breadboard next the D10 pin on the Arduino Nano. See the 7th picture for an example.
Step 6: Connecting Motor Driver to Arduino
The orientation of the robot for this step is the front end facing you. See the last picture for example.
- If your l298n motor driver has bridging clips attached to the ENA and ENB pins remove them.
- Using wires with an input and pin make the following connection to the motor driver and then to the breadboard inputs adjacent to the Arduino Nano pins.
- Attach yellow wire to ENA on the driver and insert it next to the Arduino Nano D3 pin.
- Attach green wire to IN1 on the driver and insert it next to the Arduino Nano D2 pin.
- Attach white wire to IN2 on the driver and insert it next to the Arduino Nano D4 pin.
- Attach purple wire to IN3 on the driver and insert it next to the Arduino Nano D5 pin.
- Attach grey wire to IN4 on the driver and insert it next to the Arduino Nano D8 pin.
- Attach blue wire to ENB on the driver and insert it next to the Arduino Nano D9 pin.
- Input each wire from the left motor into the OUT1 and OUT2 ports on the motor driver. Do not tighten screws, you may need to swap the wires if the motor is driving in reverse. This is called reversing polarity.
- Input each wire from the right motor into the OUT3 and OUT4 ports on the motor driver. Do not tighten screws, you may need to swap the wires if the motor is driving in reverse. This is called reversing polarity.
Note: you will secure the motor wires in Step 9: Edit and Upload Code.
Step 7: Wheels and Break
- Attach the 3D printed break pad to the underside of the robot. You can change the position of this to alter the robots performance. The break will slide into any of the three cutouts on the front of the car. See the first picture for details.
- Slide a wheel on each motor.
Step 8: MBlock
In order to write code for this robot in Scratch we will need to use the mBlock software. It is entirely free and you do not need to create an account to use it. You can download and install it at this link: www.mblock.cc/en/download
- Download and install mBlock from the link provided above.
- Download the TCPL's prewritten code here: Google Drive.
- Open mBlock once it's fully installed.
- On the bottom left Devices tab close the CyberPi device by selecting the blue x. A message box will appear asking if you want to delete this sprite. Select delete.
- Navigate back to the Devices tab and select add. A new page full of devices will appear.
- Locate the Arduino Nano and install it by selecting the blue plus symbol. Then select ok.
- Locate the Arduino Nano(old) and install it by selecting the blue plus symbol. Then select ok. We add the Arduino Nano(old) in case we cannot upload the code to the regular one.
- Now that you have both Arduino Nanos installed you can open TCPL's code. Select from the top ribbon File, Open from your computer, select Don't Save, andthen navigate to where you downloaded the code and open it.
- This will open the full code.
Step 9: Edit and Upload Code
In this Instructable, I will only go into details on how to edit the premade code from the previous step. In a later Instructable I will share an overview and lesson plan on how to write this code from the begining.
Depending on the microcontroller of your Nano you will need to use either the Arduino Nano or Arduino Nano(old) device. To figure this out we will attempt to upload code from each device in mBlock.
- Make sure the battery case on the robot is switched off.
- Insert a 9v battery into the case and lock it into place.
- Attach the USB cable to the robot and connect it to your computer.
- Select the top device. If you followed along in the last step this is the Arduino Nano.
- Click the checkbox next to Show all connectable devices, this will show which COM your robot is connected too. Select Connect under the COM. The number after COM may differ on your device and can be ignored.
- Navigate back to the Devices menu on the bottom left of the screen and select the larger Upload button to begin writing the script to the robot.
Note: If the code fails to upload you will need to navigate to the Devices menu select the bottom option, Arduino Nano (old), and repeat steps 5 and 6 above.
Once you have the code uploaded make sure the motor wires are placed into the motor driver ports, see Step 6 Connecting Motor Driver To Arduino. Turn on the robot making sure there is nothing in front of the sensors. If both wheels are driving forward you can tighten the screws above each port. If any of the motors are driving in the wrong direction swap the wires to opposite ports. If the motor continues to drive in the wrong direction make sure both sensors are not detecting any object. You may need to turn the sensitivity of the sensor(s) down if there is nothing in front of it and both indicator lights are on, see the 8th picture for an example. Take a screwdriver and gently rotate the potentiometer clockwise until only one light is on, see the 9th picture. By turning the potentiometer clockwise the sensors's sensitivity is being reduced; turning the other way increases it. Turning the dial too far in any direction will make it so the sensor either turns off or makes it always detect. If the solutions above do not work you will need to go back to Step 6 Connecting Motor Driver To Arduino and double-check that everything is connected properly.
Editing the code:
- Navigate to the Blocks area in mBlock and find the stack of Scratch code where the topmost block reads, "when Arduino starts up".
- Click, hold, and drag until you see the Sp_Forward block. You will notice comments on what input bubble controls which motor. The orientation of the robot for these comments is the rear of the robot facing you.
- Click on the left input bubble and insert any whole number between 0 and 255. Input the same number into the right bubble. We use what is known as pulse width modulation, or PWM for short, to control the speed of the robot. For more info on this click here. Basically, 0 is off and 255 is the maximum speed. You can adjust the speed of how fast you want your car to go in a straight line by inserting numbers less than 255 for example, the value of 180 will drive the motors slowly. On some tracks going at full speed will cause the car to crash through a wall. experiment with different values to get the best results.
- Locate the Sp_Left block. The first bubble controls how fast the one wheel turns forward and the other controls how fast the other wheel travels backwards. Reversing one of the wheels during a turn will cause the robot to make a sharper turn. Experiment with different turning and breaking speeds to find the optimal turn around a corner.
- Locate the Sp_Right block. The first bubble controls how fast the one wheel turns backwards and the other controls how fast the other wheel travels forwards. Reversing one of the wheels during a turn will cause the robot to make a sharper turn. Experiment with different turning and breaking speeds to find the optimal turn around a corner.
- Connect your car to your computer and upload the code to the computer. You can refer to the beginning of this section for a refresher on how to upload code to an Arduino Nano.
Step 10: Make a Track
You can make track out of almost anything as long as it's a surface that doesn't fully absorb light. The sensors detect the walls by receiving IR signals reflected off a surface. At TCPL we use a combination of recycled cardboard and foam to make the track. There is no official measurement for each track piece.
For straight pieces, I cut a length of material between 6 - 15 inches long and about 2.5 inches tall. I then cut a roughly 2.5x2.5 square and hot glue it to the back of the piece to make a stand. See the fourth picture for an example.
Note: If you would rather not use hot glue you can slightly bend straight pieces and they will stand up. You can see an example of this kind of track in the video.
When it comes to curves I will bend the material into shape and that causes it to stand without needing anything glued to it. See the last two pictures for examples.
The distance between the outer and inner walls should always be 15 inches or more. This is due to the sensor's max range of 30cm. If both sensors see a wall the robot will stop.
Once the track is set up I will place some painter's tape to mark the finish/starting line. You can find the app TCPL uses to capture lap times here: Apple or Android.
TCPL runs a very simple time attack event where participants and/or teams get 3 laps to set a personal best time. The participant and/or team with the fastest time wins for that week. TCPL will change the track layout weekly to keep things interesting.