Control a DC motor with a propeller using an Arduino

FunduinoPublished on

Geschwindigkeit und Drehrichtung eines Gleichstrommotors über eine H-Brücke steuern

Kleine Elektromotoren sind fast immer Gleichstrommotoren – im Englischen „Direct Current“, kurz DC. In Arduino-Projekten treiben sie Roboter, Propeller oder Lüfter an. Schon kleine Motoren brauchen aber mehr Strom, als ein digitaler Ausgang des Mikrocontrollers liefern kann. Deshalb steuerst du sie nicht direkt an, sondern über eine H-Brücke mit eigener Spannungsversorgung.

In dieser Anleitung baust du eine solche Schaltung auf, lässt den Motor in beide Richtungen drehen und regelst seine Geschwindigkeit anschließend mit einem Potentiometer.

Breakout board with L9110 motor driver, DC motor, and propeller
The module with L9110 H-bridge, motor, and propeller

You need this

L9110 motor driver - 2.5-12 V, 2 channels, with motor and propeller L9110 motor driver - 2.5-12 V, 2 channels, with motor and propeller 3,05 € *Item no. F23106461About the product — L9110 motor driver - 2.5-12 V, 2 channels, with motor and propeller
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm

All 3 parts together 15,91 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Why a motor needs an H-bridge

Small electric motors are usually DC motors. The English term for Gleichstrom is “direct current,” hence the name DC motor. These motors are used in many Arduino projects, for example to drive small robots or propellers.

Even small motors often need a lot of current. Arduino digital outputs, however, provide only a few milliamps - just enough for a tiny solar motor. That’s why motors are connected not directly to the output pins, but through a separate electronic component: the H-bridge.

H-bridges are available as individual components, but wiring them is cumbersome. That’s why in the Arduino world they’re usually already mounted on a board, with connections for the power supply and the microcontroller. These modules are called motor drivers or motor driver shields.

For your first experiments, there are modules that include both the motor and the H-bridge. Here, we’re using a motor-propeller module with the L9110 H-bridge.

Module connections

The propeller module has four pins:

  • GND – to the Arduino GND pin
  • VCC – to the Arduino 3.3V pin
  • INA – to Arduino pin 10
  • INB – to Arduino pin 11

This small propeller module is powered by the Arduino. However, as soon as you operate larger motors, the above applies: the H-bridge then needs its own power supply, whose ground you connect to the Arduino’s GND.

Breakout board with L9110 motor driver, DC motor, and propeller
The module with L9110 H-bridge, motor, and propeller

Control direction and speed

The H-bridge expects a PWM signal generated by the Arduino. On the UNO, these can be pins 3, 5, 6, 9, 10, and 11 – they’re marked with a wavy line (~) on the board.

The signal has 256 levels, ranging from 0 to 255. It’s sent using analogWrite(MotorA, 255): “MotorA” stands for the pin being used, and the number indicates the drive strength.

Speed and direction of rotation depend on how the two values work together: the greater the difference, the faster the motor turns. Which value is greater determines the direction.

  • Right, full speed: MotorA = 0, MotorB = 255
  • Left, full speed: MotorA = 255, MotorB = 0
  • Right, half speed: MotorA = 0, MotorB = 128
  • Left, half speed: MotorA = 128, MotorB = 0

The following sketch makes the motor turn right at full speed.

Turn the motor to the right at full speed
int MotorA = 10;  // Pin INA on digital PWM pin 10
int MotorB = 11;  // Pin INB on digital PWM pin 11

void setup()
{
  pinMode(MotorA, OUTPUT);  // Define pin 10 (Motor A) as an output
  pinMode(MotorB, OUTPUT);  // Define pin 11 (Motor B) as an output
}

void loop()
{
  // Both values range from 0 to 255. The greater the difference between
  // Motor A and Motor B, the faster the motor turns.
  analogWrite(MotorA, 0);
  analogWrite(MotorB, 255);
}

Extension: Speed adjustment via rotary knob

The module can also be controlled by an external signal - for example, a slider or rotary control that sets the speed.

In the following sketch, a potentiometer is connected to analog input A0. Its value is converted to a range of 0 to 255 with map() and passed directly to the H-bridge.

Control the speed using a potentiometer
int MotorA = 10;  // Pin INA on digital PWM pin 10
int MotorB = 11;  // Pin INB on digital PWM pin 11

// The word "input" now stands for A0. The potentiometer signal is connected there.
int input = A0;

int sensorValue = 0;       // Variable for the sensor value, initial value 0
int speed = 0;  // Variable for the speed, initial value 0

void setup()
{
  pinMode(MotorA, OUTPUT);  // Define pin 10 (Motor A) as an output
  pinMode(MotorB, OUTPUT);  // Define pin 11 (Motor B) as an output
}

void loop()
{
  // The voltage at the dial is read and stored as a number between 0 and 1023
  // under "sensorValue".
  sensorValue = analogRead(input);

  // The map command converts the range 0 to 1023 into 0 to 255. This lets
  // the value be passed directly to analogWrite.
  speed = map(sensorValue, 0, 1023, 0, 255);

  // This value stays at 0 - changing MotorB controls the speed.
  analogWrite(MotorA, 0);
  analogWrite(MotorB, speed);
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.