Last time, we controlled the ON/OFF switch of a DC motor with Arduino. Today, I’d like to delve deeper into the use of motors.
Taking radio-controlled cars as an example, motors are used not only for turning them on and off, but also for moving forward and backward, stopping, and braking. Let’s use a motor driver to control such motors.
Expected time to complete: 60 minutes
Parts needed:
Arduino base (Arduino Uno R3) https://www.adafruit.com/products/50
Breadboard https://www.adafruit.com/products/64
DC motor 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 x2 (1N4007) http://www.digikey.com/product-detail/en/1N4007-TP/1N4007-TPMSCT-ND/773694
Resistor 10KΩ×2 / 100KΩ×2
Temperature sensor (lM61CIZ) http://www.digikey.com/product-detail/en/LM61CIZ%2FNOPB/LM61CIZ%2FNOPB-ND/367306
Condenser http://www.digikey.com/product-detail/en/35ZLH100MEFC6.3X11/1189-1300-ND/3134256
AA battery holder (4 in series)
AA battery×4
Toggle switch http://www.amazon.com/250V-Position-Micro-Toggle-Switch/dp/B014XJ7PZG
Motor driver BD6222HFP
We have here a rather small part. This is the motor driver. It can come in many different forms, but we will try out this motor driver today. A motor drivers, as the name suggests, is an integrated circuit (IC) that contains circuitry for driving (controlling) motors.
Since experience is the best teacher, let’s go ahead and fiddle with actual circuit to learn how to use the motor driver.
As you will realize when you handle the part yourself, it is considerably smaller than the parts we’ve been using until now. The terminals coming out of the IC are also narrow, and it looks like you can’t connect them directly on the breadboard.
In cases like this, we will use an adapter board to be able to convert it on the breadboard. We will place the IC atop the SOP16 pin DIP adapter board and solder it on.
The BD6222HFP that we use today has terminals only on one side of the IC, so we will only use one side of the adapter board.
SOP16 pin DIP adapter board
I’ll go ahead and place the IC on the adapter board and solder it on. The spaces between the pins are very narrow, so it is more difficult than the previous soldering of parts. When soldering such ICs, it is necessary to firmly secure the IC and adapter board so they do not become misaligned when the soldering iron comes in contact.
Here, I’ve secured the adapter and the IC with a clip attached to our soldering station, but if you don’t have a clip, it’s a good idea to tape the adapter board and the IC together. If you’re doing that, however, be careful that the IC doesn’t heat up and melt the tape while you’re soldering.
As for the amount of solder, you’ll need less than half of what you’d use for a usual-sized part, so if you use too much solder and connect two pins together (as I did once this time…), you can use a desoldering braid or desoldering pump to remove it.
Soldering complete!
Now that we’re done with soldering, we will verify the function of each pin on the motor driver from the data sheet. Despite its small size, the BD6222HFP is capable of sending 6–15v of voltage and 2A of current to the motor.
VREF – Power source control for the motor (3–15v)
OUT1 – Output pin to the motor
FIN – Together with RIN, signal input for forward, backward, stop, brake, etc.
GND – Ground
RIN – Together with FIN, signal input for forward, backward, stop, brake, etc.
OUT2 – Output pin to the motor
VCC – Power source input for the motor driver (6–15v)
As we learned last time, the Arduino can’t be used as a power source for the motor. We had to add a separate battery. On the motor driver, we have separate power source input pins for the motor and for the motor driver IC.
VREF is the pin to supply power to the motor. The motor’s rotation count and such are determined by the voltage that is input here. VCC is the pin to supply power to operate the motor driver.
OUT1 and OUT2 are output pins, so we connect them to the motor. As I wrote last time, the motor can be connected without worrying about the plus/minus orientation.
FIN and RIN are the input pins for operating the motor driver. By running current of different patterns from here, we can control the motor.
Now that we know each function, we will learn how to use them while building a simple circuit.
The circuit below is a test circuit to rotate forward and backward using the motor driver. By pressing one of the left/right switches, a signal (current) is sent from FIN or RIN to the motor driver and current flows to the motor.
I am using a 100KΩ resistor.
Now, let’s substitute the switch parts with the Arduino. Now, we can use Arduino to control things from the program side such as stopping the motor rotation after a set time or switching the sense of rotation. The program below repeats between rotate → pause → reverse → pause every second.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
int leftP = 9; int rightP = 10; void setup() { pinMode(leftP, OUTPUT); pinMode(rightP, OUTPUT); } void loop() { digitalWrite(rightP, LOW); digitalWrite(leftP, HIGH); delay(1000); digitalWrite(rightP, LOW); digitalWrite(leftP, LOW); delay(1000); digitalWrite(leftP, LOW); digitalWrite(rightP, HIGH); delay(1000); digitalWrite(rightP, LOW); digitalWrite(leftP, LOW); delay(1000); } |
I am outputting to the motor using digitalWrite here. But since the rotation count changes based on the voltage input to the motor driver’s VREF, it is also possible to change the rotation count by using the analogWrite function instead. However, if the output is too weak and falls below 3v (the BD62222HFP is designed for 3–15v), the motor will just make a whining noise and will fail to rotate.
By using the analog input pin, you can use the Arduino to easily accomplish things like running the motor once a sensor (such as a light sensor) reaches a certain value.
To give a simple example, by incorporating a temperature sensor, we can create a device that automatically runs a fan when the temperature rises above a certain threshold. Today, we’ll use the LM61CIZ temperature sensor. The LM61CIZ is a commonly used temperature sensor and it can measure from -30 to 100 degrees.
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 |
int leftP = 9; int rightP = 10; void setup() { pinMode(leftP, OUTPUT); pinMode(rightP, OUTPUT); Serial.begin(9600); } void loop() { int ans = analogRead(0); int tv = map(ans,0,1023,0,5000); int temp = map(tv,300,1600,-30,100) //Begin rotation when temperature reaches 25 degrees if(temp > 25){ digitalWrite(rightP, LOW); digitalWrite(leftP, HIGH); delay(1000); digitalWrite(rightP, LOW); digitalWrite(leftP, LOW); delay(1000); digitalWrite(leftP, LOW); digitalWrite(rightP, HIGH); delay(1000); digitalWrite(rightP, LOW); digitalWrite(leftP, LOW); } Serial.println(temp); delay(1000); } |
The part written in red is the conversion equation when using the LM61CIZ with the Arduino. For today, the program simply outputs to the motor when the temperature sensor’s value is greater than 25 degrees (the if statement in blue). As long as the temperature is above 25 degrees, the motor runs continuously.
Are you now comfortable with the basic operation of motor drivers? Starting next time, I’d like to incorporate motor driver functions like stop and brake to create a simple radio-controlled (RC) car with the Arduino.
If we are to build an RC car, is one motor enough? Do we need 2 motors to control steering when turning left and right? With a larger circuit, will the RC car need to be bigger, too? These are some of the questions that will come up. So next time, as we did when we worked on the Stevenson screen, we should decide on the specs of the RC car, then build and learn.