Outils pour utilisateurs

Outils du site


issue113:python

Ceci est une ancienne révision du document !


Table des matières

1

This month, we will be using the RPi to control a simple DC Hobby motor. This can be obtained from most hobby stores, electronics suppliers, and even some big box hardware stores. Here is a “shopping list” of what we will be needing. • DC Hobby Motor • L293D Dual H-Bridge Motor Driver Chip • 4 AA (or AAA) Battery Holder and batteries • Breadboard • Male to Male jumpers • RPi (of course) Before we start wiring and coding, we need to talk about a couple of things. First, NEVER EVER connect a motor of any kind directly to the RPi. You are asking for disaster. The current requirements can cause the RPi to “melt down”. The driver chip is less than $5.00 US and is a lot cheaper than a $39.00 RPi. Second, we will discuss the L293D H-bridge motor driver for a few moments so you understand how this device works.

2

According to wikipedia, “An H bridge is an electronic circuit that enables a voltage to be applied across a load in either direction. These circuits are often used in robotics and other applications to allow DC motors to run forwards and backwards.” Here is a pinout of the driver chip (“borrowed” from hardwarefun.com)… Pins 1 and 9 are enable pins. Think of these pins as an On/Off switch. A low state on the enable pin means the motor is off. A high state means that the motor CAN BE on. Let’s look at it as a logic table or truth table. Pins 1A and 2A are one side of the chip and are control lines like the enable pins. The same logic applies to 3A and 4A (the other half of the chip) as well. Pins 1Y and 2Y are the outputs to the motor. The bottom line of the crazy table above is this. If you want the motor to turn on you MUST… • Have the Enable pin HIGH (pin 1 and/or pin 9) • AND EITHER 1A OR 2A HIGH BUT NOT BOTH (chip pin 2 and pin 7 respectively) Now that we have decoded the logic of the magic chip, we can start to wire our breadboard and RPi.

3

Wiring The Fritzing drawing (next page, top right) shows our wiring diagram for this month. Notice that we are only using one half of the chip, so we could actually control two small DC motors instead of just one. That, however, will be up to you to experiment with. As always, make the wiring connections to the RPi BEFORE you power the RPi on. Also double check your wiring, especially since we have an external power source. You might not be happy if something is on the wrong pin. This first Fritzing image shows the connections to the RPi and to the breadboard/chip. Basically it breaks down like that shown in the table bottom right The next Fritzing diagram (below) shows the battery and motor hook-ups. We are using the +5 VDC power from the RPi to power the motor driver chip (RPi pin 2 to L293D pin 16). While the above diagram shows AAA batteries, you can use a battery pack that uses AA batteries as well. We are also providing Ground from the RPi (pin 6) to the chip (pins 4,5,12,13). The motor is driven on chip pin 3 (1A) and pin 5 (2A). The battery connects to chip pin 8 to provide the voltage for the motor.

4

Code We will deal with code in two programs. The first simply turns on the motor, runs for a few seconds then stops it. The second is a modified version of the first that shows how to reverse the motor. Dcmotor1.py This program (below) will simply turn on the motor in forward (clockwise) mode and let it run, then stop it. Basically it just proves that everything is working correctly. Dcmotor2.py In this program (next page), we set up the GPIO pins just as we did before, but we are now using PWM to modulate the speed of the motor. If you don’t remember PWM, please refer to Part 64 back in FCM 107. In the forward mode, the longer the duty cycle (closer to 100) means the motor will go faster. In the reverse mode, the SHORTER the duty cycle (closer to 0) means the motor will go faster. We speed up the motor by setting the duty cycle to a LOWER percentage, let it run for 5 seconds, then stop it, do a GPIO.cleanup(), then end the program. Well, that’s it for this month. Next month, we will be working with servos. All you need is a small inexpensive one with three wires. We will not be using parts from this month’s project, but keep them for future projects. Until then, have fun.

issue113/python.1476094628.txt.gz · Dernière modification : 2016/10/10 12:17 de auntiee