Having fun and learning something new - some of the best things that we can do.
Some time ago, I wrote about arduino and some basic stuff that we can do with various elements and components. Off cause, I didn’t cover every component that I played with, but some, that can be used today I did.
My son likes to play with a car (who didn’t? ;]). Play and learn - it’s a good way to go. So we decided to build a car - “with headlights” and “with auto-pilot” and “with remote control” and “with display” and “with light-sensors” and “with buzzer” and a few more :].
Components and tools
The list of needed components are next:
L298N Motordriver x1
Ultrasonic Sensor - HC-SR04 x1
Arduino UNO x1 with USB cable x1
Sensor shield V5 x1
4WD Smart Robot Car Chassis (baseplates) x1
Wheels x4
Engines x4
Servo
Screws with nuts M3 x18
Spacers M3x60 x6
Resistor 220 Ohm x2
LED lights x2
Wires (a bunch :) - male-female, male-male, female-female
Batteries AA x4
I bought parts from different sets, and without any instruction, so some of them are not perfectly fit ;].
Also I started within the simplest variant - without remote control and any additional sensors, all modifications will be added a bit later.
Tools:
Solder iron
Screwdriver
Soft:
IDE Arduino/Visual Code
Fritzing (optional, for schematic)
In total, a minimal set of components is:
Assembling
A lot of photos!
Before we actually starts, it’s good to see how all components are connected with each other, in other words, it’s good to see the schematic:
This scheme is the simplest one and does not include IR components and BLE modules; We can simplify it even more by removing LEDs
Let’s go step-by-step.
The actual assembling I started by soldering wires to the DC motors. This process takes a few sub-steps:
Prepare motor:
connect wires:
solder wires:
Now, we can start assembling the chassis and connect motors to them.
remove protection from acrylic chassis and holders:
prepare screws and nuts:
put motors on chassis and assemble them:
Next step - add wheels:
add spacers
and add top chassis
Now, it’s time to connect DC motors to L298N bridge:
The next step is a bit messy - we should assemble servo. The components that I bought were a bit miss-aligned and can’t be assembled easily, so I manually adjust some plastic parts and then assemble the servo with its holder. After we should add an ultrasonic reader to the servo - I used wires to connect them.
The complete result shown on pics below:
After this operations the car looks like this:
The next step is to combine the Arduino board with the sensor shield. I bought v5.
The schematic for this component is next:
To use a shield, we should place it on the Arduino board and align it to the right side.
Make sure that all pins are in their places.
The final wiring looks like this:
The full car photo:
This is a minimal configuration that can be used for a car. Off cause we can use only 2 engines, we can reduce qty of wheels and makes some other modification, but this is another story.
Programming
To make our car movable, we need to add a few things.
Need to control engines (DC motors and L298N)
Use ultrasonic component to detect obstacles (servo and HC-SR04)
Enable lighting (using LEDs) if obstacles occurs
I didn’t reinvent the wheel, so just grab the code from the Arduino forum and adjust a bit for my needs (unfortunately I lost the link, but u can easily google for it).
We can go step-by-step.
Define all pins
We have already assembled the car, some pins are used on an Arduino board to control specific components. It’s time to define them in the code.
Servo
For Servo we must define 3 pins and create an object instance
It’s good to know, how servo works - according to doc, we can rotate it from 0 to 180 degrees. This mean, that we can scan for obstacle only “effective” zone - from 60 to 120 degrees. In other words:
so in code, this can be defined as next
Motor
The whole motor pins and related values can be defined as next:
Other defines
We also need to define pins for LED and store distance info. This can be done like this:
Initialization
We are ready to configure initial state for all components:
In details:
readDistance will be defined below
Loop
After everything is done and ready, we can define the main loop - scan surrounding space and move forward if space is free or backward and to some side if not.
To do so, we need to read data from an ultrasonic sensor in effective range, analyze it, and control or engines:
As I mention above, I didn’t reinvent the wheel and just grab this code from an unknown author on the Arduino forum.