Raspberry Pi Control Motor Speed

In this tutorial I will show you how to control the speed of a DC motor with your Raspberry Pi.

Controlling a motor is one of the most exciting things you can do with your Raspberry Pi. Motors are power-hungry components. They often require more power than the 3V3 your Raspberry can provide. That’s the reason we will connect the motor to a external power source (battery). You can use the same techniques described in this tutorial to work with any component, which requires external power source.

Building the Circuit

Before we can do anything we have to hook it all together. For this tutorial you will need

  • a DC motor
  • external power supply (battery) – you may need a different voltage according to your motor
  • Darlington Array chip (ULN2003) – I will give you more details on this later in this tutorial
  • a couple of wires

Look at the diagram below. It shows how to build the circuit.

raspberry-pi-connect-motor-board

And this is the schematic of our circuit

raspberry-pi-connect-motor-schematic

Darlington Array Chip

The ULN2003 is seven pairs of transistors that allows us to  switch a higher voltage with the 3V3 from a GPIO port in a safe way. We can use a standard transistor instead, but why should we bother when the ULN2003 can be ordered for less than 0.20$ and can control 7 motors at the same time.

Another important thing about ULN2003 is, that it protects your Raspberry GPIO ports from the voltage spike that can occur when an inductive load (a relay, solenoid, motor etc.) is switched off.

Controlling the Motor

We will use a software PWM to control the motor speed. If you want to know more about PWM please read my previous tutorial Raspberry Pi Dim LED with PWM and Java. We will use the Pi4j API to write our speed controlling program in Java. If you want to know how to install, configure and run projects with Pi4j please refer to this tutorial.

In Pi4j PWM values go from 0 to 100. In the following example program we will get use of this and first rotate the motor at 25% speed, than go to half speed (50%), than to full speed(100%) and finally turn it down.

import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.SoftPwm;

public class MotorPWM {

	private static int PIN_NUMBER = 1;

	public static void main(String[] args) throws InterruptedException {

		System.out.println("Started");

		// initialize wiringPi library, this is needed for PWM
		Gpio.wiringPiSetup();

		// softPwmCreate(int pin, int value, int range)
		// the range is set like (min=0 ; max=100)
		SoftPwm.softPwmCreate(PIN_NUMBER, 0, 100);

		setSpeed(25);
		setSpeed(50);
		setSpeed(100);
		setSpeed(0);

		System.out.println("Finished");
	}

	private static void setSpeed(int speed) throws InterruptedException {
		System.out.println("Speed is set to " + speed + "%");
		// softPwmWrite(int pin, int value)
		// This updates the PWM value on the given pin. The value is checked to
		// be in-range and pins that haven't previously been initialized via 
		// softPwmCreate will be silently ignored.
		SoftPwm.softPwmWrite(PIN_NUMBER, speed);
		// wait 3 seconds
		Thread.sleep(3000);
	}
}

You can run the program on Raspberry with following command:

sudo java -classpath .:classes:/opt/pi4j/lib/'*' MotorPWM

Watch the video to see the final result

 

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Digg thisPin on PinterestShare on RedditShare on StumbleUponPrint this pageEmail this to someone

Leave a Reply