Adafruit's Raspberry Pi Lesson 9. Controlling a DC Motor



Similar documents
Arduino Lesson 13. DC Motors. Created by Simon Monk

Arduino Lesson 16. Stepper Motors

Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing

Arduino Lesson 14. Servo Motors

Arduino Lesson 4. Eight LEDs and a Shift Register

Playing sounds and using buttons with Raspberry Pi

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable. Created by Simon Monk

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

Arduino Lesson 17. Sending Movement Detector

Adafruit's Raspberry Pi Lesson 7. Remote Control with VNC

Arduino Lesson 9. Sensing Light

Using an IR Remote with a Raspberry Pi Media Center

Eric Mitchell April 2, 2012 Application Note: Control of a 180 Servo Motor with Arduino UNO Development Board

Adding a Real Time Clock to Raspberry Pi

Arduino Lesson 0. Getting Started

Arduino Lesson 1. Blink

Arduino Lesson 5. The Serial Monitor

Adafruit's Raspberry Pi Lesson 6. Using SSH

Adafruit NFC/RFID on Raspberry Pi

Arduino Motor Shield (L298) Manual

Monitor Your Home With the Raspberry Pi B+

Adafruit's Raspberry Pi Lesson 1. Preparing an SD Card for your Raspberry Pi

5inch HDMI LCD User Manual

Matrix and 7-Segment LED Backpack with the Raspberry Pi

UPS PIco. to be used with. Raspberry Pi B+, A+, B, and A. HAT Compliant. Raspberry Pi is a trademark of the Raspberry Pi Foundation

Servo Info and Centering

Adafruit's Raspberry Pi Lesson 3. Network Setup

How to Perform Real-Time Processing on the Raspberry Pi. Steven Doran SCALE 13X

SSH to BeagleBone Black over USB

Crazy Alarm Clock L A K S H M I M E Y Y A P P A N J A M E S K A Y E W I L L I A M D I E H L C O N G C H E N

Copyright 2012 by Fen Logic Ltd. All rights reserved.

Bob Rathbone Computer Consultancy

Table 1 Comparison of DC, Uni-Polar and Bi-polar Stepper Motors

DC Motor control Reversing

Adafruit NFC/RFID on Raspberry Pi. Created by Kevin Townsend

Tiny Arduino Music Visualizer

cs281: Introduction to Computer Systems Lab08 Interrupt Handling and Stepper Motor Controller

How To Connect A Raspberry Pi To The Internet On A Microsoft Moonstone 2 (Microsoft) Microsoft Powerbook 2 (Powerbook 2) (Powerboard 2) And Powerbook 3 (Powerstation 2)

Servo Motors (SensorDAQ only) Evaluation copy. Vernier Digital Control Unit (DCU) LabQuest or LabPro power supply

Drive a 16x2 LCD with the Raspberry Pi

WebIOPi. Installation Walk-through Macros

PiFace Control & Display

Tutorial. replace them with cell-phone operated module. The advantages of a cell-phone operated bot are:-

Basic DC Motor Circuits. Living with the Lab Gerald Recktenwald Portland State University

Basic DC Motor Circuits

Beginner s Guide to the PI MATRIX. by Bruce E. Hall, W8BH 1) INTRODUCTION

Pololu DRV8835 Dual Motor Driver Shield for Arduino

Setting up IO Python Library on BeagleBone Black

Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout

SD21-21 Channel Servo Driver Module Power Servo's

secucam User Manual Version 1.1.x Darhon Software

Web Site: Forums: forums.parallax.com Sales: Technical:

Special Lecture. Basic Stamp 2 Programming. (Presented on popular demand)

Basic Pulse Width Modulation

Pulse Width Modulation Applications

Your Multimeter. The Arduino Uno 10/1/2012. Using Your Arduino, Breadboard and Multimeter. EAS 199A Fall Work in teams of two!

Display Board Pulse Width Modulation (PWM) Power/Speed Controller Module

Drives 101. Functions of an Adjustable Frequency Drive (AFD) This lesson covers the basic functions of an Adjustable Frequency Drives (AFD).

Table 1 below is a complete list of MPTH commands with descriptions. Table 1 : MPTH Commands. Command Name Code Setting Value Description

Adafruit MCP9808 Precision I2C Temperature Sensor Guide

UGLYDATV 0.1 by F5OEO Evariste

PiFace Real Time Clock user guide

UniPi technical documentation REV 1.1

Talon and Talon SR User Manual

Raspberry Pi radio player with touchscreen

Raspberry Pi Adding a Real Time Clock (RTC)

Accurate Measurement of the Mains Electricity Frequency

DATASHEET. ADAM Arduino Display Adaptor Module. Arduino Compatible Shield P/N: 4Display-Shield-FT843 For the 4D Systems 4DLCD-FT843 Display

Install bluez on the Raspberry Pi

Selecting and Implementing H-Bridges in DC Motor Control. Daniel Phan A

CHAPTER 11: Flip Flops

Microstack Accelerometer Getting started

Lab 17: Building a 4-Digit 7-Segment LED Decoder

EasyC. Programming Tips

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

Pulse width modulation

STEPPER MOTOR SPEED AND POSITION CONTROL

Lab 6 Introduction to Serial and Wireless Communication

revolution Contents: Introduction Power 28-pin Project Board with input/output cables

RC Camera Control. User Guide v /20/2012

Six-servo Robot Arm. DAGU Hi-Tech Electronic Co., LTD Six-servo Robot Arm

Adaptive Cruise Control

7/Basic Input and Output

In-System Programmer USER MANUAL RN-ISP-UM RN-WIFLYCR-UM

FlyingDream AAT Driver Quick Guide V1.1

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

MCP4725 Digital to Analog Converter Hookup Guide

Pmod peripheral modules are powered by the host via the interface s power and ground pins.

Chapter 4: Pulse Width Modulation

Raspberry Pi Kernel-o-Matic

INTRODUCTION TO SERIAL ARM

Networking Remote-Controlled Moving Image Monitoring System

PRODUCTIVITY THROUGH INNOVATION 600 CONTROL DIRECT DRIVE TECHNICAL/OPERATION MANUAL

Arduino Shield Manual

Qt on Raspberry Pi. Jeff Tranter Integrated Computer Solutions (ICS) Qt Developer Days

DC Motor Driver 24V 20A [RKI-1340]

Intel Do-It-Yourself Challenge Lab 2: Intel Galileo s Linux side Nicolas Vailliet

Character LCDs. Created by Ladyada. Last updated on :45:29 PM EDT

Adafruit SHT31-D Temperature & Humidity Sensor Breakout

Transcription:

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

Guide Contents Guide Contents Overview Parts Part Qty PWM The PWM Kernel Module File Description L293D Hardware Software Test & Configure 2 3 4 4 4 7 8 8 8 9 10 11 13 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. 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 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 Page 5 of 13

4 x AA or AAA battery holder and batteries. 1 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. 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 mode 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 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. frequency This 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. 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. 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. 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) 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. 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