Sub topic 11
Wiring and Programming the Driver from an Arduino
This chapter connects an Arduino to an H-bridge driver module and turns the signed-speed idea into working code. It assumes you can use pinMode, digitalWrite, and analogWrite. It also assumes you know Ohm's law, can read a wiring diagram, and know that a transistor can act as a switch. Earlier chapters covered the H-bridge switches, direction control, flyback paths, PWM duty cycle, and driver selection. Here they come together in a circuit you can build.
The examples use a dual H-bridge module with two direction inputs and one enable input per channel. Common boards are based on the L298N or L293D. Pin names vary between boards, so check the silkscreen and datasheet for your module.
What the Arduino has to supply
The Arduino never powers the motor. It sends three low-current logic signals per motor channel:
| Signal | Typical name | Job |
|---|---|---|
| Direction A | IN1 | Selects which half-bridge connects the motor terminal to supply |
| Direction B | IN2 | Selects the other half-bridge |
| Enable | EN (or ENA) | Turns the output stage on and off. PWM on this pin sets speed |
The driver takes motor power from a separate supply and passes it to the motor according to those signals. The Arduino only decides what the driver does.
Wiring
Make these connections for one motor channel. The pin numbers match the code later in the chapter.
- Arduino pin 7 to driver IN1.
- Arduino pin 8 to driver IN2.
- Arduino pin 9 to driver EN. This pin must support PWM. On an Uno or Nano, pins 3, 5, 6, 9, 10, and 11 do.
- Motor terminals to the driver's OUT1 and OUT2.
- Motor supply positive to the driver's motor supply terminal (often labeled VS, +12V, or VMOT).
- Motor supply negative to the driver's GND terminal.
- Driver GND to an Arduino GND pin.
Step 7 is the one most often missed. The Arduino's signals are voltages measured relative to its own ground. If the driver's ground is not connected to it, the driver has no reference and reads the signals unpredictably. The motor may twitch, run at random, or not move at all. The motor supply negative, the driver GND, and the Arduino GND must all meet at one common ground.
Power and decoupling
Power the motor from its own supply, such as a battery pack or a regulated adapter. Do not take motor current from the Arduino's 5 V pin. A motor can draw far more than the board's regulator or USB port can give. Stall current can be several times the running current.
Place a capacitor of 100 µF or more across the motor supply, close to the driver. Add a 100 nF ceramic capacitor close to the driver's supply pins as well. Motor switching and brush noise put spikes on the supply. Without decoupling, those spikes can reset the Arduino or corrupt its readings.
Many L298N modules have an onboard 5 V regulator selected by a jumper. Leave the jumper on only if the motor supply is within the module's stated range, usually up to about 12 V. Above that, remove the jumper and supply logic 5 V separately. Otherwise the regulator can overheat.
The truth table
Each channel's behavior follows from IN1, IN2, and EN.
| EN | IN1 | IN2 | Result |
|---|---|---|---|
| LOW | any | any | Outputs disabled, motor coasts |
| HIGH | HIGH | LOW | Forward |
| HIGH | LOW | HIGH | Reverse |
| HIGH | LOW | LOW | Brake (both terminals low) |
| HIGH | HIGH | HIGH | Brake (both terminals high) |
Some modules label the direction inputs differently, and which rotation counts as "forward" depends on how you wired the motor. If the motor spins the wrong way, swap the two motor wires or swap IN1 and IN2 in code.
This truth table matches the L298N and the L293D. Modules based on other chips, such as the TB6612FNG or DRV8871, use different pin schemes. Some have a PWM input and a direction input instead. Always read the datasheet before you reuse code.
Writing the control function
A single function that takes a signed speed keeps the rest of the program simple. The sign sets direction and the magnitude sets the PWM duty cycle.
const uint8_t IN1 = 7;
const uint8_t IN2 = 8;
const uint8_t EN = 9; // must be a PWM-capable pin
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN, OUTPUT);
}
// speed: -255 (full reverse) to 255 (full forward), 0 = stopped
void setMotor(int speed) {
speed = constrain(speed, -255, 255);
if (speed > 0) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
} else if (speed < 0) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
} else {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
analogWrite(EN, abs(speed));
}
void loop() {
setMotor(200);
delay(2000);
setMotor(0);
delay(500);
setMotor(-200);
delay(2000);
setMotor(0);
delay(500);
}Several details in this function matter.
constrainstops values outside the valid range from reachinganalogWrite. Without it,analogWrite(EN, 300)wraps in unexpected ways, andabs(-300)would give a duty cycle that is not what you asked for.- The direction pins are set before the speed. The direction is then correct as soon as the PWM begins.
- Speed zero sets both inputs LOW and the duty cycle to 0. With EN at duty 0, the output stage is disabled, so the motor coasts. If you want active braking, keep EN HIGH with both inputs at the same level, as the truth table shows.
analogWritetakes 0 to 255. A value of 0 gives a constant LOW and 255 a constant HIGH. Values between give a duty cycle ofvalue / 255.
Digital signals versus PWM on EN
If you hold EN HIGH with digitalWrite, the motor runs at full speed whenever IN1 and IN2 differ. That is a valid way to test the wiring before you add PWM. Use it for your first check: if the motor runs at full speed in both directions, the wiring and ground are correct.
Minimum starting duty
A motor does not turn at every duty cycle. Static friction and the motor's internal resistance mean that low duty values produce a hum and no motion. The threshold depends on the motor, the load, and the supply voltage. Find it by experiment: step setMotor upward from 0 until the shaft moves reliably, and note the value.
Also remember that the driver drops some voltage. A bipolar driver like the L298N can lose about 1.5 to 2 V at each switch pair, so a 6 V motor on a 7.4 V supply may see well under 5 V. The current and voltage-drop chapter covers this in detail.
Common mistakes
- EN on a non-PWM pin.
analogWriteon such a pin outputs only fully on or fully off based on the value (below 128 gives LOW, 128 and above gives HIGH on many boards). Speed control then appears to do nothing. - Missing common ground. Symptoms are erratic or absent motion.
- Powering the motor from the 5 V pin. The Arduino resets or the motor barely moves.
- Reversing at full speed with no pause. This causes current spikes and mechanical shock. The next chapter shows how to avoid it.
- Reusing the same pins as other libraries. The Servo library, for example, disables PWM on pins 9 and 10 on an Uno. If you use it, put EN on a different PWM pin.
Testing in steps
Bring up the circuit in a sequence so each fault shows up on its own.
- Power the Arduino from USB with the motor supply off. Confirm the sketch uploads.
- Switch on the motor supply with the motor disconnected. Check that nothing on the driver gets warm.
- Connect the motor and hold EN HIGH with
digitalWriteto confirm both directions. - Switch EN to
analogWriteand try several speeds. - Add ramping and other features only after these steps work.
Exercise
Your motor does not move, and the driver LED is on. The code calls setMotor(200). You measure 12 V at the driver's motor supply terminal. The Arduino is powered over USB, and the motor supply negative is connected only to the driver's GND terminal. What is the most likely cause, and how do you fix it?
Show answer
The grounds are not shared. The Arduino's logic signals have no common reference with the driver. Connect a wire from the driver's GND (which is tied to the motor supply negative) to an Arduino GND pin, then run the sketch again.
Exercise
In setMotor, why does the function use abs(speed) for analogWrite instead of passing speed directly, and what would happen to a negative speed value if you did not use abs?
Show answer
Direction is already set by the IN1 and IN2 pins, so the PWM value only needs the magnitude. analogWrite expects 0 to 255. A negative value would be converted to an unsigned value and produce a wrong duty cycle, usually near full speed, so reverse would not follow the intended speed.