sssemil
Posts: 10
Joined: Wed Feb 19, 2014 10:44 am

2 GPIO pins to control motor

Mon Jul 14, 2014 9:14 am

Right now I use the below scheme to control motor, but if I change + and - it will not work. How can I change scheme, so when I change + and - on gpios + and - will change on out? Thanks
Attachments
10558737_626172487490007_833474441_o.jpg
10558737_626172487490007_833474441_o.jpg (45.99 KiB) Viewed 2140 times


User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: 2 GPIO pins to control motor

Mon Jul 14, 2014 9:39 am

Hi,
sssemil wrote:Right now I use the below scheme to control motor,
I don't believe that this circuit works at all (that you can make the motor run with any state of GPIO signals). But this is good, because if the motor starts to run (if you slightly modify the circuit), there is a high risk that you burn the RasPi with this approach.

Don't play with matches... obtain a motor driver chip (or a board containing it) and add it between RasPi and the motor.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

Duane Degn
Posts: 55
Joined: Thu Jul 10, 2014 6:30 am
Location: Idaho, USA

Re: 2 GPIO pins to control motor

Wed Jul 16, 2014 8:13 pm

As others have said, you need an H-bridge. I'd suggest you read the article about H-bridges linked to earlier.

Pololu makes a bunch of different h-bridge boards. These newer boards don't waste as much power as heat as some of the older h-bridge chips like the L298, L293D or the SN754410 chip.

There are a lot of cheap (about $3) L298N boards on ebay. While these aren't the best h-bridges they do work just fine if wired correctly. I posted a video on YouTube not long ago showing how to make these connections.

I used the L298N with a 3.3V microcontroller, and I believe the connections shown in the video are safe for the Raspberry Pi. If you get bored with the video, I hope you at least watch the end. I still think the robot I show off at the end is pretty cool.

In the video, I showed how to control one motor with three I/O pins. There are a couple of different ways to control a motor with the cheap L298N boards with only two I/O pins.

I didn't discuss the two pin methods in the video (at least not much) so I'll mention them here.

IMO, the best way to control a motor with just two I/O pins if you add an inverter to the circuit (I generally use a transistor). I'll try to find a schematic of how to set this sort of circuit up and add a link to this thread (unless someone bets me to it). With an inverter in the circuit one GPIO pin is used to set both IN (direction) pins. One direction pin is set directly by the I/O pin and the other direction pin is automatically set to the opposite of the first by the inverter.

The price of using an inverter is you lose the ability to set both direction pins low (or both high) to dynamically brake the motor. There is usually enough friction in a small robot's gearbox and drive chain to make dynamic braking unneeded. Using an inverter, one GPIO pin sets the direction the motor and the other GPIO pin controls the speed by pulsing (PWM) the enable pin. By pulsing the enable pin, the motor will coast between high pulses.

The other way to control a motor with only two I/O pins is to tie the enable pin high and use the two direction pins (e.g. IN1 and IN2) to control both direction and speed. One direction pins is set low (which pin depends on the direction) and other is pulsed (PWM). This sounds like an easy way to control the motor with just two GPIO pins but the problem with this method is each low pulse of the PWM signal causes the motor to brake.

By pulsing the enable pin (when using three GPIO pins or when using an inverter), the motor coasts between high pulses. By letting the motor coast between high pulses, the h-bridge chip stays cooler and less current is required to drive the motor.

There is debate about the pulse/brake vs pulse/coast methods and I can give links to discussion on this topic if anyone is interested.

Whenever possible test the various techniques yourself to see which works best in your application.

One thing that's important when driving inductive loads (like a DC motor) is to have flyback diodes in the circuit. The cheap L298N boards have these diodes on the board so you don't need to add them yourself.

Some h-bridge chips/circuits have ways to monitoring the current but I'll try to keep this already long post from getting longer by letting you research the topic yourself (or let others discuss it).

Duane Degn
Posts: 55
Joined: Thu Jul 10, 2014 6:30 am
Location: Idaho, USA

Re: 2 GPIO pins to control motor

Thu Jul 17, 2014 8:03 pm

A fellow forum member alerted me to a website which has a truth table for the L298N which contradicted with this section of my above ramblings on H-bridges.
Duane Degn wrote:The other way to control a motor with only two I/O pins is to tie the enable pin high and use the two direction pins (e.g. IN1 and IN2) to control both direction and speed. One direction pins is set low (which pin depends on the direction) and other is pulsed (PWM). This sounds like an easy way to control the motor with just two GPIO pins but the problem with this method is each low pulse of the PWM signal causes the motor to brake.
Let's see if I can copy the table.

Code: Select all

Motor A truth table
ENA	IN1	IN2	Description
0	  N/A	N/A	Motor A is off
1	  0	  0	  Motor A is off (floating)     ****** This is incorrect. ******
1	  0	  1	  Motor A is on and turning backwards
1	  1	  0	  Motor A is on and turning forwards
1	  1	  1	  Motor A is stopped (brakes)
The above table copied from Banana Robotics does not agree with the L298N datasheet (nor with practical testing). Figure 6 on page 6 of the datasheet also includes a truth table. In the datasheet's truth table, it shows with enable high, the function will be "fast motor stop" when IN1 = IN2. They call IN1 and IN2 "C" and "D" in the truth table (it looks like Fig 6 uses the pins to motor B to illustrate the example so "C" and "D" are really IN3 and IN4, but the logic applies to both motors).

Edit to add: Banana Robotics told me they would fix the error later today.

Return to “Automation, sensing and robotics”