![](https://voltageandcurrent.wordpress.com/wp-content/uploads/2021/11/two_sensor_arduino_circuit.png?w=1024)
This is the example circuit we looked at in class. An Arduino Nano reads signals from two TCRT5000 infrared reflective sensors and then control two DC motors via an SN754410NW quad half H-bridge integrated circuit (IC).
We also looked at the physical layout of the pins on the SN754410NE:
- The two centre pins on each side of the chip are ground connections.
- The pins either side of the ground pins are outputs.
- Each output pin is controlled by the input pin right next to it (on the other side to the ground pin).
- Corner pins 8 and 16 are connected to the positive voltage supply. Different voltages can be connected to these two pins where the motors require a higher voltage than the control circuit, but that is not the case here.
![](https://voltageandcurrent.wordpress.com/wp-content/uploads/2021/11/sn754410ne.png?w=1024)
This code example illustrates the use of a new function to control the direction of both motors (forward, reverse or stop) with a single function call. The benefit of doing this is that the low-level details about which pins need to be set high and low are encapsulated in the motors function, which makes the loop function much more readable.
//
// 2-sensor control of Arduino robot
// Written by Ted Burke - last updated 17-Nov-2021
//
void setup()
{
// motor control pins
pinMode(2, OUTPUT); // left motor forward
pinMode(3, OUTPUT); // left motor reverse
pinMode(4, OUTPUT); // right motor forward
pinMode(5, OUTPUT); // right motor reverse
// Open serial connection to PC
Serial.begin(9600);
}
void loop()
{
motors(1,1); // left motor forward, right motor forward
delay(2000);
motors(-1,-1); // left motor reverse, right motor reverse
delay(2000);
motors(1,0); // left motor forward, right motor stop
delay(2000);
}
// This function sets the direction of both motors
void motors(int left, int right)
{
// Control left motor
if (left > 0)
{
digitalWrite(2, HIGH); // left motor forward
digitalWrite(3, LOW);
}
else if (left < 0)
{
digitalWrite(2, LOW); // left motor reverse
digitalWrite(3, HIGH);
}
else
{
digitalWrite(2, LOW); // left motor stop
digitalWrite(3, LOW);
}
// Control right motor
if (right > 0)
{
digitalWrite(4, HIGH); // right forward
digitalWrite(5, LOW);
}
else if (right < 0)
{
digitalWrite(4, LOW); // right motor reverse
digitalWrite(5, HIGH);
}
else
{
digitalWrite(4, LOW); // right motor stop
digitalWrite(5, LOW);
}
}