One of the most important things your RoboSumo robot needs to be able to do is control its motors. This basic example shows how to control a single DC motor, but it can be easily extended to control two motors, which will allow your robot to drive forward and backwards, but also to steer left and right.
The motors draw significantly more current from the voltage supply, so you will not be able to power the motors from the USB socket of the PC. You will need to include a battery pack. Ultimately, this will also allow your robot to drive around independently (without a wire connecting it to the PC).
To perform bi-directional control of a DC motor (i.e. driving the motor forwards and backwards), two digital output pins must be used on the Arduino. To enable pins as digital outputs, use the same "pinMode" function that you used for the LED Flash Challenge.
The motor requires a larger current than an Arduino digital output pin can supply directly. Therefore, an integrated circuit (IC) is required between the Arduino and the motor. The IC you received for this purpose in your RoboSumo kit is a 16-pin microchip called the SN754410NE. Have a look at the SN754410NE datasheet to find out which pins are which.
The SN754410NE is a so-called quad half H-bridge. Basically, what this means is that it has four separate control channels - each with an input pin and an output pin. The Arduino sends low power control signal into the input pin and that controls a higher power signal coming out of the output pin.
To achieve bi-directional control of a motor, two channels will be used at once - one for each terminal of the motor.
Since the SN754410NE has four channels, we can use it to perform bi-directional control of two motors.
This circuit allows bi-directional control of one DC motor by the Arduino:
Here's how my example breadboard looks. Please don't rely on the breadboard photos to build your own circuit. It's useful to see a photo at this stage, because you are still learning how to lay out a breadboard circuits, but for future examples a circuit diagram may be the only thing provided, so you need to learn how to translate the circuit diagram into a working breadboard circuit yourself.
The following code can be used to test the circuit:
//
// Basic motor control example for Arduino Nano
// Motor forward for 2 seconds, reverse for 2 seconds
// and stop for 4 seconds. Repeats indefinitely./
//
// Written by Ted Burke - 01-Jan-2023
//
void setup()
{
pinMode(4, OUTPUT); // make pin D4 a digital output
pinMode(5, OUTPUT); // make pin D5 a digital output
}
void loop()
{
// Motor forward for 2 seconds
digitalWrite(4, HIGH); // set voltage on pin D4 to 5V
digitalWrite(5, LOW); // set voltage on pin D5 to 0V
delay(2000);
// Motor reverse for 2 seconds
digitalWrite(4, LOW); // set voltage on pin D4 to 0V
digitalWrite(5, HIGH); // set voltage on pin D5 to 5V
delay(2000);
// Motor stop for 4 seconds
digitalWrite(4, LOW); // set voltage on pin D4 to 0V
digitalWrite(5, LOW); // set voltage on pin D5 to 0V
delay(4000);
}
Notes on connecting the SN754410NE
The two centre pins on each side of the chip (pins 4, 5, 12 and 13) should be connected to ground (0 V) using black wire.
Pins 8 and 16 should be connected to the positive supply voltage (6 V) using red wire.
The input signal on pin 2 controls the output signal on pin 3.
The input signal on pin 7 controls the output signal on pin 6.
The input signal on pin 10 controls the output signal on pin 11.
The input signal on pin 15 controls the output signal on pin 14.
One motor can be connected to pins 3 and 6 (controlled by signals on pins 2 and 7).
Another motor can be connected to pins 11 and 14 (controlled by signals on pins 10 and 15).
Pins 1 and 9 can be left unconnected.
Notes on wire colours / wire preparation
Use black wire for any ground connections (0 V). Anywhere you see black wire, that should tell you that the voltage is 0 V.
Use red wire for any positive supply voltage connections in the circuit (6 V in this particular circuit). Anywhere you see red wire, that should tell you that the voltage is 6 V.
Do not use red or black for any other connections on the circuit. You can decide the other colours you use for different types of connections.
When stripping wire to plug into the breadboard, the length of insulation removed at each end should be about 6mm. If you strip too much off, you will leave metal exposed above the surface of the breadboard, which could cause a short circuit. If you strip too little wire off, there is a danger that the wire will not connect properly with the metal conductor inside the breadboard.