In the last article, we completed the body of the RC car. But, during the building process, we had some issues. Today, we will continue working on the RC car controller. As the Arduino UNO was too large to mount on the car, we will use an Arduino Pro Mini instead.
Expected time to complete: 90 minutes
Parts needed:
Arduino Pro mini (Arduino Pro Mini 328 5V 16MHz https://www.adafruit.com/products/2378)
FTDI-USB Serial converter cable (http://www.ftdichip.com/Products/Cables/USBTTLSerial.htm)
Breadboard (https://www.adafruit.com/products/64)
DC motor (FA-130RA-227 https://www.pololu.com/product/77)
Motor driver (BD6222HFP http://www.digikey.com/product-detail/en/BD6222HFP-TR/BD6222HFPCT-ND/1936319)
SOP16 pin DIP adapter board (https://www.sparkfun.com/products/498)
Diode (1N4007 http://www.digikey.com/product-detail/en/1N4007-TP/1N4007-TPMSCT-ND/773694)×2
Resistor 10KΩ×2 / 100KΩ×2
Tact switch
Joystick (https://www.parallax.com/product/27800)
Condenser (100μF http://www.digikey.com/product-detail/en/35ZLH100MEFC6.3X11/1189-1300-ND/3134256)
AA battery box (4 in series)
AA battery×4
Toggle switch (http://www.amazon.com/250V-Position-Micro-Toggle-Switch/dp/B014XJ7PZG)
LEGO blocks – as necessary
8T pinion gear (http://www.amazon.com/15289-Mini-Metal-Plastic-Pinion/dp/B002CAT2TG)
We will start by making the controller for the RC car.
Last time, we could rotate the car’s wheels left and right and go forward and backward. So, we will build the controller to control these movements. About the program on the Arduino side, we will translate the analog values input via the controller parts into movement.
Today, we will use a tact switch and a joystick as the controller parts.
Picture 1 Tact Switch
As you can tell from its name, a tact switch is a type of switch. It makes a clicking sound when you press it, and returns to its original position when you lift your finger. This type of switch is used when we want it to be ON only when it’s being pressed.
Picture 2 Joystick
The joystick is a well-known part as it’s used to play video games. the joystick is a part consisting of two potentiometers, arranged along the horizontal and vertical axes. When the stick is moved, the potentiometers activate and the resistance changes. On the Arduino, we can determine the state of the joystick based on this change in resistance.
Picture 3 The controller will look something like this
Picture 4 Controller
The tact switches will be used for the forward and the backward movement. The joystick will be used for steering left and right. The joystick can be moved along both X and Y axis. Since we are only using it for steering this time, we will only using it on the X axis.
The circuit will look like the following.
Figure 1 Circuit for the controller
To verify the input from the switches and the joystick, we will run a sample program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//******************************************* // Controller Sample Program //******************************************* int aPin = 2; int bPin = 4; int cPin = A2; void setup() { Serial.begin(9600); Serial.println("setup"); pinMode(aPin,INPUT); pinMode(bPin,INPUT); } void loop() { Serial.print("JOYSTICK:"); Serial.println(analogRead(A2)); if(digitalRead(aPin) == HIGH){ Serial.println("A"); } if(digitalRead(bPin) == HIGH){ Serial.println("B"); } delay(100); } |
The joystick will provide analog input values between 0–1023. For a joystick, the initial value is when the stick is centered along the horizontal dimension (X axis). With our current circuit, the value 512 will be displayed when the joystick is at rest.
For the tact switch, the circuit is complete only when it is pressed, at which point the contents of the if statements are evaluated.
Once we’ve confirmed that the controller is functional, we’ll integrate it with the RC car itself. The circuit will look like the following figure.
Figure 2 RC car circuit
It’s getting a bit complex and a little confusing at first glance. But it’s built upon components we’re familiar with. So, we only need to be careful about incorrectly connecting the jump wires.
Picture 5 The basic setup is complete
The final program will be as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <Servo.h> // Load servo motor library Servo myservo; // Create an object for the servo int val; // Variable to store servo angle int aPin = 2; int bPin = 4; int cPin = 9; int dPin = 10; void setup() { myservo.attach(11); // Set digital pin 11 as output pin for servo angle commands Serial.begin(9600); Serial.println("setup"); pinMode(aPin,INPUT); pinMode(bPin,INPUT); pinMode(cPin,OUTPUT); pinMode(dPin,OUTPUT); } void loop() { int st = analogRead(A2); // 0-1023, center is 512 float st2 = (float)st/1023; val = st2*90; val = val+45; myservo.write(val); //Move servo based on joystick value Serial.print("SERVO:"); Serial.print(st2); Serial.print("-"); Serial.println(val); if(digitalRead(aPin) == HIGH){ Serial.println("A"); digitalWrite(cPin,HIGH); digitalWrite(dPin,LOW); } else if(digitalRead(bPin) == HIGH){ Serial.println("B"); digitalWrite(cPin,LOW); digitalWrite(dPin,HIGH); } else{ digitalWrite(cPin,LOW); digitalWrite(dPin,LOW); } delay(100); } |
We’ve got to the point where we have the basic functionality of our RC car.
However, it is inconvenient in its current form because the Arduino is too big.
Now, I’d now like to implement several ideas to make the RC car smaller and neater.
Until now, we have been using Arduino UNO. But it’s too big for the RC car. Hence, today we will work on the miniaturization by using an Arduino Pro Mini.
As I mentioned in the first article, there are different types of Arduino. Today we’ll try out the Arduino Pro mini, which is among the smaller types of Arduino. One word about these compatible boards. As the program to write on the Arduino chips is publicly available, anyone can create his own Arduino by using few components and using a supported chip (such as AVR microcontrollers). Thanks to this, many Arduino boards, compatible with the official versions, are sold. If a board is out of stock, you might want to check something different from the official version. And you can also find something cheaper. It might be worth searching for compatible boards.
Picture 6 Comparison between Arduino UNO (top) and Arduino Pro Mini (bottom)
The major differences between the Arduino UNO and the Arduino Pro mini are the following.
One of the important point is that the Arduino Pro Mini comes in two operating voltage versions.For example, when using a servo that operates at 5v, we can operate it with the 5v Arduino without any external voltage. But, with a 3.3v Arduino, the voltage is insufficient and it won’t run! When using the Arduino Pro mini, we must choose between the 5v or 3.3v version depending on the use case.
It operates in the same way as the Arduino UNO. Only some settings are different, as shown in the following figure.
Figure 3 In “Tools” → “Board”, select the Arduino Pro Mini
Figure 4 In “Tools”→ “Serial Port”, select serial cable via USB
Once configured, it can be used just like the Arduino UNO. So, we’ll test it out with the LED blinking.
Picture 7 LED blinking with the Arduino Pro mini
Once the LED blinking is successful, we’ll transfer the RC car circuit that we made on the Arduino UNO to the Arduino Pro mini.
By “transfer”, we only need to reconnect the jump wires according to the pin numbers, so this work is quickly done.
Picture 8 We finished the transfer from the Arduino UNO to the Arduino Pro mini
Picture 9 Mounting on top of the body
The RC car is now complete!
There are some other ways to make it even lighter and more compact. We can find infinite possibilities, such as replacing the LEGO car body with other parts. Or we could solder the components on the breadboard to a circuit board. As I want to build and RC car using different sensors’ features, I’ll introduce you various type of sensors next time.