SlideShare a Scribd company logo
1 of 13
Download to read offline
Adafruit's Raspberry Pi Lesson 9. Controlling a DC Motor
Created by Simon Monk
Last updated on 2014-04-17 09:00:29 PM EDT
2
3
4
4
4
7
8
8
8
9
10
11
13
Guide Contents
Guide Contents
Overview
Parts
Part
Qty
PWM
The PWM Kernel Module
File
Description
L293D
Hardware
Software
Test & Configure
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 2 of 13
Overview
This lesson describes how to control both the speed and direction of a DC motor using Python
and a L293D chip.
In Lesson 8, we used the Pi to generate pulses to control the position of a servo motor. In this
lesson we use pulses to control the speed of a regular DC motor and the L293D motor control
chip to reverse the direction of the current through the motor and hence the direction in which it
turns.
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 3 of 13
Parts
To build this project, you will need the following parts.
Part Qty
Raspberry Pi 1
Cobbler Breakout Board 1
Set of male to male jumper leads 1
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 4 of 13
Half-size breadboard 1
6V DC Motor 1
L293D Motor Controller IC 1
Adafruit Occidentalis 0.2 or later
operating system distribution.
1
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 5 of 13
4 x AA or AAA battery holder and
batteries.
1
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 6 of 13
PWM
Pulse Width Modulation (or PWM) is a technique for controlling power. We use it here to control
the amount of power going to the motor and hence how fast it spins.
The diagram below shows the signal from the PWM pin of the Raspberry Pi.
Every 1/500 of a second, the PWM output will produce a pulse. The length of this pulse controls
the amount of energy that the motor gets. No pulse at all and the motor will not turn, a short
pulse and it will turn slowly. If the pulse is active for half the time, then the motor will receive half
the power it would if the pulse stayed high until the next pulse came along.
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 7 of 13
The PWM Kernel Module
Adafruit and Sean Cross have created a kernal module that is included with the
Occidentalis (http://adafru.it/aQx) distribution. For details of creating an Occidentalis follow this
link (http://adafru.it/aWq). If you want to use the module with Raspbian or some other
distribution, then there is help on installing the kernel module into your environment
here (http://adafru.it/aQx).
You used the PWM and Servo Kernel Module in Lesson 8, to control a Servo. This time, you will
use the same module to control the speed of the motor.
The modules interface uses a file type of interface, where you control what the output pin and
therefore the servo is doing, by reading and writing to special files. This makes it really easy to
interface with in Python or other languages.
The files involved in using the module to drive a servo are listed below. All the files can be
found in the directory /sys/class/rpi-pwm/pwm0/
File Description
active
This will be 1 for active 0 for inactive. You can read it to find out if the output pin is
active or write it to make it active or inactive.
delayed
If this is set to 1, then any changes that you make to the other files will have no
effect until you use the file above to make the output active
mode
Write to this file to set the mode of the pin to either pwm, servo or audio. Obviously
we want it to be pwm. Note that the pin is also used by the Pi's audio output, so you
cannot use sound at the same time as controlling a motor.
frequencyThis is the number of pulses to be produced per second.
duty
The value here needs to be between 0 and 100 and represents the percentage of
the pulse during which the power to the motor is on. The higher the number, the
faster the motor will spin.
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 8 of 13
L293D
This is a very useful chip. It can actually control two motors independently. We are just using
half the chip in this lesson, most of the pins on the right hand side of the chip are for controlling
a second motor, but with the Raspberry Pi, we only have one PWM output.
The L293D has two +V pins (8 and 16). The pin '+Vmotor (8) provides the power for the
motors, and +V (16) for the chip's logic. We have connected pin 16 to the 5V pin of the Pi and
pin 8 to a battery pack.
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 9 of 13
Hardware
There are two reasons why we need to use a L293D chip in this project. The first is that the
output of the Raspberry Pi is nowhere near strong enough to drive a motor directly and to try
this may damage your Raspberry Pi.
Secondly, in this lesson, we want to control the direction of the motor as well as its speed. This
is only possible by reversing the direction of the current through the motor, something that the
L293D is designed to do, with the help of two control pins.
The project fits nicely on a half-sized breadboard.
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 10 of 13
Software
Since we need to control pins (connected to pin 4 and 17 of the GPIO) we also need to use the
GPIO library. For details of this, see Lesson 4 (http://adafru.it/aTH).
There are lots of ways to get the sketch from the listing below onto your Raspberry Pi. Perhaps
the easiest is to connect to your Pi using SSH (See Lesson 6 (http://adafru.it/aWc)) opening an
editor using the command below:
nano motor.py
and then pasting in the code below, before saving the files using CTRL-x.
Here is the code:
import RPi.GPIO as io
io.setmode(io.BCM)
in1_pin = 4
in2_pin = 17
io.setup(in1_pin, io.OUT)
io.setup(in2_pin, io.OUT)
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 11 of 13
def set(property, value):
try:
f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
f.write(value)
f.close()
except:
print("Error writing to: " + property + " value: " + value)
set("delayed", "0")
set("mode", "pwm")
set("frequency", "500")
set("active", "1")
def clockwise():
io.output(in1_pin, True)
io.output(in2_pin, False)
def counter_clockwise():
io.output(in1_pin, False)
io.output(in2_pin, True)
clockwise()
while True:
cmd = raw_input("Command, f/r 0..9, E.g. f5 :")
direction = cmd[0]
if direction == "f":
clockwise()
else:
counter_clockwise()
speed = int(cmd[1]) * 11
set("duty", str(speed))
he Python program first sets-up the two GPIO pins to be outputs. It then defines the same
convenience function (“set”) that we used in Lesson 8, to write to the PWM Kernel Module. This
is then used to set the parameters for PWM.
There are two other functions defined, “clockwise” and “counter_clockwise”, that control the
direction of the motor by changing the two input pins.
If both control pins are HIGH, or both LOW then the motor will be stopped. But if IN1 is HIGH and
IN2 LOW it rotates in one direction, reversing in direction if the values of IN1 and IN2 are
reversed.
The main loop of the program waits for you to enter commands in the format of a letter (“f” or
“r”) and a digit between 0 and 9. The letter sets the direction of the motor and the digit the
speed, by multiplying the digit by 11 to give a value between 0 and 99 that the PWM library
expects.
© Adafruit Industries
https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-
dc-motor
Page 12 of 13
Test & Configure
To run the program, you will need to run it as super user to get access to the GPIO pins, so
type:
sudo python motor.py
You will then get a prompt for you to enter a command. Try entering a few 2 letter commands
as shown below.
$sudo python motor.py
Command, f/r 0..9, E.g. f5 :f9
Command, f/r 0..9, E.g. f5 :r4
Command, f/r 0..9, E.g. f5 :
© Adafruit Industries Last Updated: 2014-04-17 09:00:30 PM EDT Page 13 of 13

More Related Content

What's hot

Tesla hacking presentation 'jaarbeurs World of Technology and Science' Octobe...
Tesla hacking presentation 'jaarbeurs World of Technology and Science' Octobe...Tesla hacking presentation 'jaarbeurs World of Technology and Science' Octobe...
Tesla hacking presentation 'jaarbeurs World of Technology and Science' Octobe...Jasper Nuyens
 
Tesla hacking presentation
Tesla hacking presentationTesla hacking presentation
Tesla hacking presentationJasper Nuyens
 
AUTOSAR 403 CAN Stack
AUTOSAR 403 CAN StackAUTOSAR 403 CAN Stack
AUTOSAR 403 CAN StackRania Nabil
 
Autosar software component
Autosar software componentAutosar software component
Autosar software componentFarzad Sadeghi
 

What's hot (7)

2502s
2502s2502s
2502s
 
Tesla hacking presentation 'jaarbeurs World of Technology and Science' Octobe...
Tesla hacking presentation 'jaarbeurs World of Technology and Science' Octobe...Tesla hacking presentation 'jaarbeurs World of Technology and Science' Octobe...
Tesla hacking presentation 'jaarbeurs World of Technology and Science' Octobe...
 
STM32 Microcontroller Clocks and RCC block
STM32 Microcontroller Clocks and RCC blockSTM32 Microcontroller Clocks and RCC block
STM32 Microcontroller Clocks and RCC block
 
Tesla hacking presentation
Tesla hacking presentationTesla hacking presentation
Tesla hacking presentation
 
AUTOSAR 403 CAN Stack
AUTOSAR 403 CAN StackAUTOSAR 403 CAN Stack
AUTOSAR 403 CAN Stack
 
Autosar software component
Autosar software componentAutosar software component
Autosar software component
 
Doc2502
Doc2502Doc2502
Doc2502
 

Viewers also liked

Дайджест вакансий второй площадки ИТ-парка в г. Набережные Челны
Дайджест вакансий второй площадки ИТ-парка в г. Набережные ЧелныДайджест вакансий второй площадки ИТ-парка в г. Набережные Челны
Дайджест вакансий второй площадки ИТ-парка в г. Набережные Челныitpark-kazan
 
IdeaHacks - Mover's edge
IdeaHacks - Mover's edgeIdeaHacks - Mover's edge
IdeaHacks - Mover's edgedpseud1
 
2016 Roof Pricing Guide
2016 Roof Pricing Guide2016 Roof Pricing Guide
2016 Roof Pricing GuideAustin Jewell
 
Qué es el bicentenario aula 2
Qué es el bicentenario   aula 2Qué es el bicentenario   aula 2
Qué es el bicentenario aula 2Veronica Pintos
 
Six "Wicked Questions" Every Leader Must Ask
Six "Wicked Questions" Every Leader Must AskSix "Wicked Questions" Every Leader Must Ask
Six "Wicked Questions" Every Leader Must Askmikegggg
 
2013: what about sustainability?
2013: what about sustainability?2013: what about sustainability?
2013: what about sustainability?MGTissues
 
Candy :D
Candy :DCandy :D
Candy :DBre15
 
Pérez yeximar tema1b.doc
Pérez yeximar tema1b.docPérez yeximar tema1b.doc
Pérez yeximar tema1b.docyeximar perez
 
Operadores basicos de Google
Operadores basicos de GoogleOperadores basicos de Google
Operadores basicos de Googlereyser2017
 
5 transformational leadership as jazz
5 transformational leadership as jazz5 transformational leadership as jazz
5 transformational leadership as jazzmikegggg
 
Orginal Training Report
Orginal Training ReportOrginal Training Report
Orginal Training ReportHafez Fayad
 
Palmprint verification using lagrangian decomposition and invariant interest
Palmprint verification using lagrangian decomposition and invariant interestPalmprint verification using lagrangian decomposition and invariant interest
Palmprint verification using lagrangian decomposition and invariant interestDakshina Kisku
 

Viewers also liked (15)

BF+H Summer ’12
BF+H Summer ’12BF+H Summer ’12
BF+H Summer ’12
 
Lumbar
LumbarLumbar
Lumbar
 
Дайджест вакансий второй площадки ИТ-парка в г. Набережные Челны
Дайджест вакансий второй площадки ИТ-парка в г. Набережные ЧелныДайджест вакансий второй площадки ИТ-парка в г. Набережные Челны
Дайджест вакансий второй площадки ИТ-парка в г. Набережные Челны
 
IdeaHacks - Mover's edge
IdeaHacks - Mover's edgeIdeaHacks - Mover's edge
IdeaHacks - Mover's edge
 
2016 Roof Pricing Guide
2016 Roof Pricing Guide2016 Roof Pricing Guide
2016 Roof Pricing Guide
 
Qué es el bicentenario aula 2
Qué es el bicentenario   aula 2Qué es el bicentenario   aula 2
Qué es el bicentenario aula 2
 
Team Work & Leadership
Team Work & LeadershipTeam Work & Leadership
Team Work & Leadership
 
Six "Wicked Questions" Every Leader Must Ask
Six "Wicked Questions" Every Leader Must AskSix "Wicked Questions" Every Leader Must Ask
Six "Wicked Questions" Every Leader Must Ask
 
2013: what about sustainability?
2013: what about sustainability?2013: what about sustainability?
2013: what about sustainability?
 
Candy :D
Candy :DCandy :D
Candy :D
 
Pérez yeximar tema1b.doc
Pérez yeximar tema1b.docPérez yeximar tema1b.doc
Pérez yeximar tema1b.doc
 
Operadores basicos de Google
Operadores basicos de GoogleOperadores basicos de Google
Operadores basicos de Google
 
5 transformational leadership as jazz
5 transformational leadership as jazz5 transformational leadership as jazz
5 transformational leadership as jazz
 
Orginal Training Report
Orginal Training ReportOrginal Training Report
Orginal Training Report
 
Palmprint verification using lagrangian decomposition and invariant interest
Palmprint verification using lagrangian decomposition and invariant interestPalmprint verification using lagrangian decomposition and invariant interest
Palmprint verification using lagrangian decomposition and invariant interest
 

Similar to Adafruit's Raspberry Pi DC Motor Control

Robotics Report final.compressed (1)
Robotics Report final.compressed (1)Robotics Report final.compressed (1)
Robotics Report final.compressed (1)Kael Kristjanson
 
amrapali builders @@ hardware hacking and robotics using the raspberry pi.pdf
amrapali builders @@ hardware hacking and robotics using the raspberry pi.pdfamrapali builders @@ hardware hacking and robotics using the raspberry pi.pdf
amrapali builders @@ hardware hacking and robotics using the raspberry pi.pdfamrapalibuildersreviews
 
Programable logic controller.pdf
Programable logic controller.pdfProgramable logic controller.pdf
Programable logic controller.pdfsravan66
 
Raspberry Pi Based GPS Tracking System and Face Recognition System.
Raspberry Pi Based GPS Tracking System and Face Recognition System.Raspberry Pi Based GPS Tracking System and Face Recognition System.
Raspberry Pi Based GPS Tracking System and Face Recognition System.Ruthvik Vaila
 
Raspberry pi tutorial
Raspberry pi tutorialRaspberry pi tutorial
Raspberry pi tutorialImad Rhali
 
Plc and hmi based stenter machine poster
Plc and hmi based stenter machine posterPlc and hmi based stenter machine poster
Plc and hmi based stenter machine posterRakshita Upadhyay
 
Automated Speed Drive using raspberry pi
Automated Speed Drive using raspberry piAutomated Speed Drive using raspberry pi
Automated Speed Drive using raspberry piVISHNUG39
 
Control robotic module using LIFA
Control robotic module using LIFAControl robotic module using LIFA
Control robotic module using LIFAVatsal N Shah
 
The Basics of programming
The Basics of programmingThe Basics of programming
The Basics of programming692sfrobotics
 
Bottle Filling Application using Arduino
Bottle Filling Application using ArduinoBottle Filling Application using Arduino
Bottle Filling Application using ArduinoParth Patel
 
IT Essentials (Version 7.0) - ITE Chapter 7 Exam Answers
IT Essentials (Version 7.0) - ITE Chapter 7 Exam AnswersIT Essentials (Version 7.0) - ITE Chapter 7 Exam Answers
IT Essentials (Version 7.0) - ITE Chapter 7 Exam AnswersITExamAnswers.net
 
IRJET - Implementation of SDC: Self-Driving Car based on Raspberry Pi
IRJET - Implementation of SDC: Self-Driving Car based on Raspberry PiIRJET - Implementation of SDC: Self-Driving Car based on Raspberry Pi
IRJET - Implementation of SDC: Self-Driving Car based on Raspberry PiIRJET Journal
 
FPGA based synchronous multi-channel PWM generator for humanoid robot
FPGA based synchronous multi-channel PWM generator for humanoid robot FPGA based synchronous multi-channel PWM generator for humanoid robot
FPGA based synchronous multi-channel PWM generator for humanoid robot IJECEIAES
 
Cnc 3axis-shield
Cnc 3axis-shieldCnc 3axis-shield
Cnc 3axis-shieldhandson28
 
IRJET- IOT Based Surveillance Robotic Car using Raspberry PI
IRJET- IOT Based Surveillance Robotic Car using Raspberry PIIRJET- IOT Based Surveillance Robotic Car using Raspberry PI
IRJET- IOT Based Surveillance Robotic Car using Raspberry PIIRJET Journal
 
Sitrains7 1200pwmpid-150301123045-conversion-gate01
Sitrains7 1200pwmpid-150301123045-conversion-gate01Sitrains7 1200pwmpid-150301123045-conversion-gate01
Sitrains7 1200pwmpid-150301123045-conversion-gate01confidencial
 
Sitrain s7 1200 pwm pid
Sitrain s7 1200 pwm pidSitrain s7 1200 pwm pid
Sitrain s7 1200 pwm pidconfidencial
 

Similar to Adafruit's Raspberry Pi DC Motor Control (20)

Robotics Report final.compressed (1)
Robotics Report final.compressed (1)Robotics Report final.compressed (1)
Robotics Report final.compressed (1)
 
amrapali builders @@ hardware hacking and robotics using the raspberry pi.pdf
amrapali builders @@ hardware hacking and robotics using the raspberry pi.pdfamrapali builders @@ hardware hacking and robotics using the raspberry pi.pdf
amrapali builders @@ hardware hacking and robotics using the raspberry pi.pdf
 
Programable logic controller.pdf
Programable logic controller.pdfProgramable logic controller.pdf
Programable logic controller.pdf
 
Raspberry Pi Based GPS Tracking System and Face Recognition System.
Raspberry Pi Based GPS Tracking System and Face Recognition System.Raspberry Pi Based GPS Tracking System and Face Recognition System.
Raspberry Pi Based GPS Tracking System and Face Recognition System.
 
Raspberry pi tutorial
Raspberry pi tutorialRaspberry pi tutorial
Raspberry pi tutorial
 
Plc and hmi based stenter machine poster
Plc and hmi based stenter machine posterPlc and hmi based stenter machine poster
Plc and hmi based stenter machine poster
 
Automated Speed Drive using raspberry pi
Automated Speed Drive using raspberry piAutomated Speed Drive using raspberry pi
Automated Speed Drive using raspberry pi
 
Control robotic module using LIFA
Control robotic module using LIFAControl robotic module using LIFA
Control robotic module using LIFA
 
Dp s7300
Dp s7300Dp s7300
Dp s7300
 
The Basics of programming
The Basics of programmingThe Basics of programming
The Basics of programming
 
New Microsoft Office Word Document
New Microsoft Office Word DocumentNew Microsoft Office Word Document
New Microsoft Office Word Document
 
Bottle Filling Application using Arduino
Bottle Filling Application using ArduinoBottle Filling Application using Arduino
Bottle Filling Application using Arduino
 
IT Essentials (Version 7.0) - ITE Chapter 7 Exam Answers
IT Essentials (Version 7.0) - ITE Chapter 7 Exam AnswersIT Essentials (Version 7.0) - ITE Chapter 7 Exam Answers
IT Essentials (Version 7.0) - ITE Chapter 7 Exam Answers
 
IRJET - Implementation of SDC: Self-Driving Car based on Raspberry Pi
IRJET - Implementation of SDC: Self-Driving Car based on Raspberry PiIRJET - Implementation of SDC: Self-Driving Car based on Raspberry Pi
IRJET - Implementation of SDC: Self-Driving Car based on Raspberry Pi
 
FPGA based synchronous multi-channel PWM generator for humanoid robot
FPGA based synchronous multi-channel PWM generator for humanoid robot FPGA based synchronous multi-channel PWM generator for humanoid robot
FPGA based synchronous multi-channel PWM generator for humanoid robot
 
Cnc 3axis-shield
Cnc 3axis-shieldCnc 3axis-shield
Cnc 3axis-shield
 
Start with arduino
Start with arduinoStart with arduino
Start with arduino
 
IRJET- IOT Based Surveillance Robotic Car using Raspberry PI
IRJET- IOT Based Surveillance Robotic Car using Raspberry PIIRJET- IOT Based Surveillance Robotic Car using Raspberry PI
IRJET- IOT Based Surveillance Robotic Car using Raspberry PI
 
Sitrains7 1200pwmpid-150301123045-conversion-gate01
Sitrains7 1200pwmpid-150301123045-conversion-gate01Sitrains7 1200pwmpid-150301123045-conversion-gate01
Sitrains7 1200pwmpid-150301123045-conversion-gate01
 
Sitrain s7 1200 pwm pid
Sitrain s7 1200 pwm pidSitrain s7 1200 pwm pid
Sitrain s7 1200 pwm pid
 

More from Stefan Oprea

Training-Book-Samsung.ppt
Training-Book-Samsung.pptTraining-Book-Samsung.ppt
Training-Book-Samsung.pptStefan Oprea
 
PRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.pptPRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.pptStefan Oprea
 
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptxStefan Oprea
 
Web technologies-course 11.pptx
Web technologies-course 11.pptxWeb technologies-course 11.pptx
Web technologies-course 11.pptxStefan Oprea
 
Web technologies-course 10.pptx
Web technologies-course 10.pptxWeb technologies-course 10.pptx
Web technologies-course 10.pptxStefan Oprea
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptxStefan Oprea
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptxStefan Oprea
 
Web technologies-course 07.pptx
Web technologies-course 07.pptxWeb technologies-course 07.pptx
Web technologies-course 07.pptxStefan Oprea
 
Web technologies-course 06.pptx
Web technologies-course 06.pptxWeb technologies-course 06.pptx
Web technologies-course 06.pptxStefan Oprea
 
Web technologies-course 05.pptx
Web technologies-course 05.pptxWeb technologies-course 05.pptx
Web technologies-course 05.pptxStefan Oprea
 
Web technologies-course 04.pptx
Web technologies-course 04.pptxWeb technologies-course 04.pptx
Web technologies-course 04.pptxStefan Oprea
 
Web technologies-course 03.pptx
Web technologies-course 03.pptxWeb technologies-course 03.pptx
Web technologies-course 03.pptxStefan Oprea
 
Web technologies-course 02.pptx
Web technologies-course 02.pptxWeb technologies-course 02.pptx
Web technologies-course 02.pptxStefan Oprea
 
Web technologies-course 01.pptx
Web technologies-course 01.pptxWeb technologies-course 01.pptx
Web technologies-course 01.pptxStefan Oprea
 
Fundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.pptFundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.pptStefan Oprea
 
Orthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.pptOrthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.pptStefan Oprea
 
Modulation tutorial.ppt
Modulation tutorial.pptModulation tutorial.ppt
Modulation tutorial.pptStefan Oprea
 
Comparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.pptComparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.pptStefan Oprea
 
OFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.pptOFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.pptStefan Oprea
 
Concepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.pptConcepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.pptStefan Oprea
 

More from Stefan Oprea (20)

Training-Book-Samsung.ppt
Training-Book-Samsung.pptTraining-Book-Samsung.ppt
Training-Book-Samsung.ppt
 
PRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.pptPRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.ppt
 
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
 
Web technologies-course 11.pptx
Web technologies-course 11.pptxWeb technologies-course 11.pptx
Web technologies-course 11.pptx
 
Web technologies-course 10.pptx
Web technologies-course 10.pptxWeb technologies-course 10.pptx
Web technologies-course 10.pptx
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptx
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptx
 
Web technologies-course 07.pptx
Web technologies-course 07.pptxWeb technologies-course 07.pptx
Web technologies-course 07.pptx
 
Web technologies-course 06.pptx
Web technologies-course 06.pptxWeb technologies-course 06.pptx
Web technologies-course 06.pptx
 
Web technologies-course 05.pptx
Web technologies-course 05.pptxWeb technologies-course 05.pptx
Web technologies-course 05.pptx
 
Web technologies-course 04.pptx
Web technologies-course 04.pptxWeb technologies-course 04.pptx
Web technologies-course 04.pptx
 
Web technologies-course 03.pptx
Web technologies-course 03.pptxWeb technologies-course 03.pptx
Web technologies-course 03.pptx
 
Web technologies-course 02.pptx
Web technologies-course 02.pptxWeb technologies-course 02.pptx
Web technologies-course 02.pptx
 
Web technologies-course 01.pptx
Web technologies-course 01.pptxWeb technologies-course 01.pptx
Web technologies-course 01.pptx
 
Fundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.pptFundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.ppt
 
Orthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.pptOrthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.ppt
 
Modulation tutorial.ppt
Modulation tutorial.pptModulation tutorial.ppt
Modulation tutorial.ppt
 
Comparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.pptComparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.ppt
 
OFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.pptOFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.ppt
 
Concepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.pptConcepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.ppt
 

Recently uploaded

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Recently uploaded (20)

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 

Adafruit's Raspberry Pi DC Motor Control

  • 1. Adafruit's Raspberry Pi Lesson 9. Controlling a DC Motor Created by Simon Monk Last updated on 2014-04-17 09:00:29 PM EDT
  • 2. 2 3 4 4 4 7 8 8 8 9 10 11 13 Guide Contents Guide Contents Overview Parts Part Qty PWM The PWM Kernel Module File Description L293D Hardware Software Test & Configure © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 2 of 13
  • 3. Overview This lesson describes how to control both the speed and direction of a DC motor using Python and a L293D chip. In Lesson 8, we used the Pi to generate pulses to control the position of a servo motor. In this lesson we use pulses to control the speed of a regular DC motor and the L293D motor control chip to reverse the direction of the current through the motor and hence the direction in which it turns. © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 3 of 13
  • 4. Parts To build this project, you will need the following parts. Part Qty Raspberry Pi 1 Cobbler Breakout Board 1 Set of male to male jumper leads 1 © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 4 of 13
  • 5. Half-size breadboard 1 6V DC Motor 1 L293D Motor Controller IC 1 Adafruit Occidentalis 0.2 or later operating system distribution. 1 © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 5 of 13
  • 6. 4 x AA or AAA battery holder and batteries. 1 © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 6 of 13
  • 7. PWM Pulse Width Modulation (or PWM) is a technique for controlling power. We use it here to control the amount of power going to the motor and hence how fast it spins. The diagram below shows the signal from the PWM pin of the Raspberry Pi. Every 1/500 of a second, the PWM output will produce a pulse. The length of this pulse controls the amount of energy that the motor gets. No pulse at all and the motor will not turn, a short pulse and it will turn slowly. If the pulse is active for half the time, then the motor will receive half the power it would if the pulse stayed high until the next pulse came along. © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 7 of 13
  • 8. The PWM Kernel Module Adafruit and Sean Cross have created a kernal module that is included with the Occidentalis (http://adafru.it/aQx) distribution. For details of creating an Occidentalis follow this link (http://adafru.it/aWq). If you want to use the module with Raspbian or some other distribution, then there is help on installing the kernel module into your environment here (http://adafru.it/aQx). You used the PWM and Servo Kernel Module in Lesson 8, to control a Servo. This time, you will use the same module to control the speed of the motor. The modules interface uses a file type of interface, where you control what the output pin and therefore the servo is doing, by reading and writing to special files. This makes it really easy to interface with in Python or other languages. The files involved in using the module to drive a servo are listed below. All the files can be found in the directory /sys/class/rpi-pwm/pwm0/ File Description active This will be 1 for active 0 for inactive. You can read it to find out if the output pin is active or write it to make it active or inactive. delayed If this is set to 1, then any changes that you make to the other files will have no effect until you use the file above to make the output active mode Write to this file to set the mode of the pin to either pwm, servo or audio. Obviously we want it to be pwm. Note that the pin is also used by the Pi's audio output, so you cannot use sound at the same time as controlling a motor. frequencyThis is the number of pulses to be produced per second. duty The value here needs to be between 0 and 100 and represents the percentage of the pulse during which the power to the motor is on. The higher the number, the faster the motor will spin. © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 8 of 13
  • 9. L293D This is a very useful chip. It can actually control two motors independently. We are just using half the chip in this lesson, most of the pins on the right hand side of the chip are for controlling a second motor, but with the Raspberry Pi, we only have one PWM output. The L293D has two +V pins (8 and 16). The pin '+Vmotor (8) provides the power for the motors, and +V (16) for the chip's logic. We have connected pin 16 to the 5V pin of the Pi and pin 8 to a battery pack. © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 9 of 13
  • 10. Hardware There are two reasons why we need to use a L293D chip in this project. The first is that the output of the Raspberry Pi is nowhere near strong enough to drive a motor directly and to try this may damage your Raspberry Pi. Secondly, in this lesson, we want to control the direction of the motor as well as its speed. This is only possible by reversing the direction of the current through the motor, something that the L293D is designed to do, with the help of two control pins. The project fits nicely on a half-sized breadboard. © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 10 of 13
  • 11. Software Since we need to control pins (connected to pin 4 and 17 of the GPIO) we also need to use the GPIO library. For details of this, see Lesson 4 (http://adafru.it/aTH). There are lots of ways to get the sketch from the listing below onto your Raspberry Pi. Perhaps the easiest is to connect to your Pi using SSH (See Lesson 6 (http://adafru.it/aWc)) opening an editor using the command below: nano motor.py and then pasting in the code below, before saving the files using CTRL-x. Here is the code: import RPi.GPIO as io io.setmode(io.BCM) in1_pin = 4 in2_pin = 17 io.setup(in1_pin, io.OUT) io.setup(in2_pin, io.OUT) © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 11 of 13
  • 12. def set(property, value): try: f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w') f.write(value) f.close() except: print("Error writing to: " + property + " value: " + value) set("delayed", "0") set("mode", "pwm") set("frequency", "500") set("active", "1") def clockwise(): io.output(in1_pin, True) io.output(in2_pin, False) def counter_clockwise(): io.output(in1_pin, False) io.output(in2_pin, True) clockwise() while True: cmd = raw_input("Command, f/r 0..9, E.g. f5 :") direction = cmd[0] if direction == "f": clockwise() else: counter_clockwise() speed = int(cmd[1]) * 11 set("duty", str(speed)) he Python program first sets-up the two GPIO pins to be outputs. It then defines the same convenience function (“set”) that we used in Lesson 8, to write to the PWM Kernel Module. This is then used to set the parameters for PWM. There are two other functions defined, “clockwise” and “counter_clockwise”, that control the direction of the motor by changing the two input pins. If both control pins are HIGH, or both LOW then the motor will be stopped. But if IN1 is HIGH and IN2 LOW it rotates in one direction, reversing in direction if the values of IN1 and IN2 are reversed. The main loop of the program waits for you to enter commands in the format of a letter (“f” or “r”) and a digit between 0 and 9. The letter sets the direction of the motor and the digit the speed, by multiplying the digit by 11 to give a value between 0 and 99 that the PWM library expects. © Adafruit Industries https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a- dc-motor Page 12 of 13
  • 13. Test & Configure To run the program, you will need to run it as super user to get access to the GPIO pins, so type: sudo python motor.py You will then get a prompt for you to enter a command. Try entering a few 2 letter commands as shown below. $sudo python motor.py Command, f/r 0..9, E.g. f5 :f9 Command, f/r 0..9, E.g. f5 :r4 Command, f/r 0..9, E.g. f5 : © Adafruit Industries Last Updated: 2014-04-17 09:00:30 PM EDT Page 13 of 13