Get tutorials Raspberry Pi Super Kit V2.0 for RaspberryPi

Lesson 7 How to Drive a DC Motor

Introduction

In this lesson, we will learn to how to use L293D to drive a DC motor and make it rotate clockwise and counterclockwise.


Components

- 1* Raspberry Pi

- 1* Breadboard

- 1* L293D

- 1* DC motor

- 1* Power Module

- Jumper wires


Principle

L293D       

This is a very practical chip that can independently control two DC motors. In this experiment, just about half of the chip is used – most pins on the right side of the chip are used to control a second motor, which is not needed since we apply only one motor here.


 

L293D has two pins (Vcc1 and Vcc2) for power supply. Vcc2 is used to supply power for the motor, while Vcc1 to supply for the chip. Since a small-sized DC motor is used here, connect both pins to +5V. If you use a higher power motor, you need to connect Vcc2 to an external power supply. At the same time, the GND of L293D should be connected to that of the SunFounder Uno board.


DC Motors are devices that turn DC electrical energy into mechanical energy. They are widely used in electrical drive for their superior speed regulation performance.


In this experiment, connect two IOs of the Raspberry Pi to two optocouplers which isolate the power supply to protect your Raspberry Pi from interference and crashing when the motor works. Connect the output port of the optocoupler to the driver IC (L293D) of the motor. When one of the two IOs of the Raspberry Pi is at high level and the other is low, the motor will rotate clockwise or counterclockwise. By adjusting the pulse width, you can realize DC motor speed regulation as well. A motor is an inductive device. Its four diodes play an important role in freewheeling and suppress the surge voltage of motor coils to prevent irreversible damages to driver ICs.


 

The module comes with a yellow jumper cap, which is for selecting between power off and voltages (3.3V and 5V). You can switch the output voltage (0/3.3/5V) by putting the cap into the exact pin.


In this experiment, it needs large currents to drive the motor especially when it starts and stops, which will severely interfere with the normal work of Raspberry Pi. Therefore, we separately supply power for the motor by this module to make it run safely and steadily. 



Experimental Procedures

Step 1: Build the circuit


 


For C language users:

Step 2: Change directory

    cd /home/pi/Sunfounder_SuperKit_C_code_for_RaspberryPi/07_Motor/

Step 3: Compile

    gcc motor.c -o motor -lwiringPi

Step 4: Run

    sudo ./motor

For Python users:

Step 2: Change directory

    cd /home/pi/Sunfounder_SuperKit_Python_code_for_RaspberryPi/

Step 3: Run

    sudo python 07_motor.py

Now, you should see the motor blade rotating.


 

Further Exploration

You can use buttons to control the clockwise and counterclockwise rotation of the motor blade based on the previous lessons. Also you can apply the PWM technology to control the rotation.


Summary

Through this lesson, you have learnt the relative principle and driving mode of DC motors, as well as how to drive a motor by Raspberry Pi. You should also pay special attention to the fact that a DC motor will greatly interfere with the whole circuit when it works, so you need to adopt photoelectric isolation and provide separate power supply. A freewheeling diode is also necessary for the whole system to work reliably and steadily. 

 

 


C Code

 /**********************************************************************
* Filename    : motor.c
* Description : Controlling a motor.
* Author      : Robot
* E-mail      : support@sunfounder.com
* website     : www.sunfounder.com
* Date        : 2014/08/27
**********************************************************************/
#include <wiringPi.h>
#include  <stdio.h>

#define MotorPin1    0
#define MotorPin2    1
#define MotorEnable  2

int main(void)
{
	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}
	
	pinMode(MotorPin1, OUTPUT);
	pinMode(MotorPin2, OUTPUT);
	pinMode(MotorEnable, OUTPUT);

	int i;

	while(1){
		digitalWrite(MotorEnable, HIGH);
		digitalWrite(MotorPin1, HIGH);
		digitalWrite(MotorPin2, LOW);
		for(i=0;i<3;i++){
			delay(1000);
		}

		digitalWrite(MotorEnable, LOW);
			delay(1000);

		digitalWrite(MotorEnable, HIGH);
		digitalWrite(MotorPin1, LOW);
		digitalWrite(MotorPin2, HIGH);
		for(i=0;i<3;i++){
			delay(1000);
		}

		digitalWrite(MotorEnable, LOW);
                        delay(1000);

	}

	return 0;
}


Python Code

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

MotorPin1   = 11    # pin11
MotorPin2   = 12    # pin12
MotorEnable = 13    # pin13

def setup():
	GPIO.setmode(GPIO.BOARD)          # Numbers GPIOs by physical location
	GPIO.setup(MotorPin1, GPIO.OUT)   # mode --- output
	GPIO.setup(MotorPin2, GPIO.OUT)
	GPIO.setup(MotorEnable, GPIO.OUT)
	GPIO.output(MotorEnable, GPIO.LOW) # motor stop

def loop():
	while True:
		print 'Press Ctrl+C to end the program...'
		GPIO.output(MotorEnable, GPIO.HIGH) # motor driver enable
		GPIO.output(MotorPin1, GPIO.HIGH)  # clockwise
		GPIO.output(MotorPin2, GPIO.LOW)
		time.sleep(5)
		
		GPIO.output(MotorEnable, GPIO.LOW) # motor stop
		time.sleep(5)
		
		GPIO.output(MotorEnable, GPIO.HIGH) # motor driver enable
		GPIO.output(MotorPin1, GPIO.LOW)   # anticlockwise
		GPIO.output(MotorPin2, GPIO.HIGH)
		time.sleep(5)
		
		GPIO.output(MotorEnable, GPIO.LOW) # motor stop
		time.sleep(5)

def destroy():
	GPIO.output(MotorEnable, GPIO.LOW) # motor stop
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()


Video


Copyright © 2012 - 2016 SunFounder. All Rights Reserved.


Previous chapter: Lesson 6 Buzzer

Next chapter: Lesson 8 Rotary Encoder

SunFounder
Apr 02 2017 at 03:42 am



© Developed by CommerceLab