6
26/9/2015 Haydn’s Maker Blog http://www.nlogn.com/blog/ 1/6 November 11, 2013 Haydn’s Maker Blog Index: A Tale of Two Motor Controllers (or Controlling a Chicken Coop Door with an Arduino: L9958 vs. L298N) Driving an HY-DIV268N-5A with a Raspberry Pi A Tale of Two Motor Controllers (or Controlling a Chicken Coop Door with an Arduino: L9958 vs. L298N) The SONXUA SX8847 motor controller offers quite a lot! The eBay listing said 240 watt high-power H-bridge PWM motor driver module/smart driver for Arduino, for only $13.88! In addition it has programmable current limits, an SPI interface, and all kinds of indicators for under voltage, over voltage, over temperature, open load, etc. In brief, it offers lots of fancy features, and at a very reasonable price. Here is a link to the eBay auction I used: http://www.ebay.com/itm/111153750411 The other motor controller I tried is based on the simpler L298N, which offers two H-bridges, but less amperage. It is even less expensive at $5.99 (including shipping). Here is the link I used: http://www.ebay.com/itm/290899895209 The chicken coop door I designed is 18"x18", and made out of 1/4" plywood. Other people have designed similar systems (here is one with a particularly clever locking door ), and they used relays to control a cordless drill motor to raise and lower the door (link ). For my design, I wanted to control the door more carefully (avoiding letting it bump against its stops), so I decided to try using these motor controllers with a cordless Ryobi 12v drill I picked up for $5 at a garage sale. I used a cadmium-sulfide photocell to detect when to open and close the door. Open when it has been consistently light for the last 60 seconds, and close if it has been consistently dark. I used three switches to detect where the door currently is. One at the very top, one almost at the top, and the third in the middle (which triggers when the top of the door passes by it and stops depressing the switch). I probably should have used two switches in the middle — one to detect when to decelerate from full to slow speed, and the second to detect when to stop, however I only had three on hand. The reason I didn’t place the lower switches at the bottom, was I wanted them to be as far as possible from the chickens, mud and other sources of trouble. Also, I arranged the almost top and middle switches so that the levers are hanging down, so that they will be less likely to have rain water drip into them. I suppose I could have used magnets and Hall effect sensors, but this is one of my first outdoor Arduino project, and I wanted to keep it simple to start with. At first when I was working on this project I favored the L9958 because of all of its fancy features. I felt more secure with all the different protections and diagnostics it offered, but I found myself struggling to get it to actually work! I only found one reference on the net for how to work with the SPI interface (which is why I decided to write this and share my code). The first problem I had was

Haydn’s Maker Blog

Embed Size (px)

DESCRIPTION

stepper motor

Citation preview

Page 1: Haydn’s Maker Blog

26/9/2015 Haydn’s Maker Blog

http://www.nlogn.com/blog/ 1/6

November 11, 2013

Haydn’s Maker BlogIndex:

A Tale of Two Motor Controllers (or Controlling a Chicken Coop Door with an Arduino: L9958 vs. L298N)

Driving an HY-DIV268N-5A with a Raspberry Pi

A Tale of Two Motor Controllers (or Controlling a Chicken Coop Doorwith an Arduino: L9958 vs. L298N)

The SONXUA SX8847 motor controller offers quite a lot! The eBaylisting said 240 watt high-power H-bridge PWM motor drivermodule/smart driver for Arduino, for only $13.88! In addition it hasprogrammable current limits, an SPI interface, and all kinds ofindicators for under voltage, over voltage, over temperature, openload, etc. In brief, it offers lots of fancy features, and at a veryreasonable price.

Here is a link to the eBay auction I used:http://www.ebay.com/itm/111153750411

The other motor controller I tried is based on the simpler L298N, which offers two H-bridges, butless amperage. It is even less expensive at $5.99 (including shipping).

Here is the link I used: http://www.ebay.com/itm/290899895209

The chicken coop door I designed is 18"x18", and made out of 1/4" plywood. Other people havedesigned similar systems (here is one with a particularly clever locking door), and they used relaysto control a cordless drill motor to raise and lower the door (link). For my design, I wanted to controlthe door more carefully (avoiding letting it bump against its stops), so I decided to try using thesemotor controllers with a cordless Ryobi 12v drill I picked up for $5 at a garage sale.

I used a cadmium-sulfide photocell to detect when to open and close the door. Open when it hasbeen consistently light for the last 60 seconds, and close if it has been consistently dark.

I used three switches to detect where the door currently is. One at the very top, one almost at thetop, and the third in the middle (which triggers when the top of the door passes by it and stopsdepressing the switch). I probably should have used two switches in the middle — one to detectwhen to decelerate from full to slow speed, and the second to detect when to stop, however I onlyhad three on hand. The reason I didn’t place the lower switches at the bottom, was I wanted them tobe as far as possible from the chickens, mud and other sources of trouble. Also, I arranged thealmost top and middle switches so that the levers are hanging down, so that they will be less likelyto have rain water drip into them. I suppose I could have used magnets and Hall effect sensors, butthis is one of my first outdoor Arduino project, and I wanted to keep it simple to start with.

At first when I was working on this project I favored the L9958 because of all of its fancy features. Ifelt more secure with all the different protections and diagnostics it offered, but I found myselfstruggling to get it to actually work! I only found one reference on the net for how to work with theSPI interface (which is why I decided to write this and share my code). The first problem I had was

Page 2: Haydn’s Maker Blog

26/9/2015 Haydn’s Maker Blog

http://www.nlogn.com/blog/ 2/6

that the 5v Vcc is somehow connected to the 12v Vss, but it shouldn’t be! Even if the Arduino was

separately powered, if I tried to change the motor’s speed from 0 to 100%, it would go into anendless cycle of resetting the Arduino. The solution to this was to run the motor at reduced power.This actually worked pretty well, however after a while I decided that taking ~40 seconds to raise orlower the door was not quite to my liking, so I tried to have the motor accelerate to full power, andthen decelerate back to half power before stopping. Now the good thing is that when controlling themotor with L9958 at half power, it would stop on a dime. However, even though it would happilyswitch speeds from 0% to 50%, 50% to 100%, and 50% to 0%, it would halt and becomeunresponsive when commanded to switch from 100% to 50%! In this case the SPI interface didn’tprovide any useful debugging information. Resetting the device did cure the problem, but that isundesireable. Also, the L9958 caused the the motor to shake when run at other speeds besides0%, 50% and 100%, so gradually ramping the speed from 50% up to 100% didn’t work well, andramping the speed from 100% down to 50% always caused the lockup. Argh! This drove me to trythe L298N.

The L298N simply worked. It worked at a wide variety of speeds, itsmoothly ramped between speeds, and the only problem I had wasthat I couldn’t find a speed where it would reliably lift and lower thedoor, and stop on a dime. To solve this problem, I decided to start ata slow initial speed (a brushed DC motor draws the maximumcurrent when starting, so starting it slowly keeps it from initiallydrawing too much current), and ramping up to full speed within asecond, and then down to an extremely slow speed, before stoppingand reversing for a brief period (to avoid overrunning where I wantedit to stop). This raises or lowers the door in less than 10 seconds.

So in summary, the advantages of the L9958 are:

Handles a lot more power: 240 watts vs. 25 watts, or 8.6 amps vs. 3 amps.Vss was somehow connected to Vcc, which caused multiple mysterious problems, and

ultimately made me abandon using this controller!Unable to smoothly and reliably drive my motor at arbitrary speeds. There is something oddabout the way the PWM cannot smoothly control the motor’s speed.SPI interface can provide debugging information.SPI interface can configure the maximum current to supply to the motor.Protection from over voltage, under voltage, etc.At half-power, the motor stopped on a dime — it didn’t coast at all.At half-power, the motor raised and lowered the door at exactly the same speed.At half-power, the motor had sufficient torque to reliably move the door both up and down.At full power it ran the motor substantially faster than the L298N could.Works with both 5v and 3.3v devicesOne thing to note: whenever the L9958 detects a problem, like under voltage, it sets an errorflag and won’t to drive the motor until the flag is reset!

The advantages of the L298N are:

Can independently control two 2 amp motors, or you can gang the inputs/outputs to drive onemotor which uses up to 3 amps.

Page 3: Haydn’s Maker Blog

26/9/2015 Haydn’s Maker Blog

http://www.nlogn.com/blog/ 3/6

Could control one stepper motor.Worked smoothly and reliably at every speed!Less expensive.Simpler interface, requiring only 3 pins vs. the L9958 which required 8 pins, leaving only onespare digital I/O pin on my Arduino!Required slightly more complicated programming (such as different minimum speeds forraising and lowering the door, reversing after stopping to prevent the motor from coasting,and gradually accelerating from the minimum speeds to full speed).

In the end, I decided that the L298N would be best for my application!

Here is a link to the Arduino sketch for the L298N version of my chicken coop door controller:ChickenDoor_L298N.ino

For comparison, here is a link to the Arduino sketch for the L9958 version of my chicken coop doorcontroller: ChickenDoor_L9958.ino

A few notes:

If you are going to use the L9958 version, you might try disconnecting Vcc between the

Arduino and SX8847 — perhaps it will work more reliably that way.The programs use what I would call a state machine paradigm: at any given moment

doorStatus indicates what the system should be doing: raising or lowering. The amount oflight the photocell detects determines when to shift between these two. When the shifthappens, the program changes the doorStatus, the motorSpeed is set, and the door moveswhile it continues to monitor the switches every millisecond. This makes the program simplerand more symmetric, and also makes it easier to detect errors, such as if the motor hasbeen running too long, or the wrong switches get triggered.Sometimes I used the original Ryobi 17v 400ma wall wart with the L9958 (and USB powerfor the Arduino). However, other times I tested it with a PC power supply — even with thelarge number of amperes supplied by the 12v and 5v rails from the PC power supply, I stillcouldn’t get the L9958 to behave reliably. The L298N worked well with separate 12v 1A or17v 400ma (motor) and 9v (Arduino) power supplies, as well as with a single 12v 2A wallwart.I removed all three jumpers on the L298N, so that the enable pins could be driven by theArduino’s PWM and the 5v would be supplied by the Arduino.To quickly test opening the door, have light shine on the photocell and press the Arduino’sreset button. To quickly test closing the door, cover the photocell and again press theArduino’s reset button.

I sure learned a lot of lessons in the course of this project!

Many thanks to the folks from Sparkfun and Maui Makers who put on an Arduino course here onMaui!

I’d originally planned to do this project using relays and a Raspberry Pi, and they mentioned that anL298N might be sufficient. My original measurements with this drill’s motor indicated that it drawsup to 7 amps, which is why I tried using the L9958 first.

Page 4: Haydn’s Maker Blog

26/9/2015 Haydn’s Maker Blog

http://www.nlogn.com/blog/ 4/6

August 18, 2013Driving an HY-DIV268N-5A with a Raspberry Pi

The HY-DIV268N-5A is an inexpensive, powerful and flexible steppermotor controller which can drive motors rated between 200ma and 5amps, at volages between 12v and 50v. Using a higher voltage ismore desirable, because it makes the steppers run faster.

Here is a link to the one I purchased on eBay for $22.84, includingshipping: http://www.ebay.com/itm/221245537239I ordered it on Sunday July 28 and it arrived on Friday August 9, 10business days later. Not bad!

I couldn't find much useful documentation online on the Toshiba TB6600 chip, which is at the heartof this device, and the Chinglish instructions from the manufacturer weren't much help, either.

On YouTube, I found a two minute video by some chaps with English accents who said to tie all ofthe lines EN+, DIR+, and PUL+ (enable, direction, and pulse) high (to 5v), and then they drove theEN-, DIR-, and PUL- lines using a transistor and pulled them to ground. Unfortunately, messing withtransistors is outside of my expertise. Here is the link: http://www.youtube.com/watch?v=mvxOuDw4JeY

I found a fellow who mentioned that he'd tied EN+, EN-, DIR-, and PUL- to ground, and then usedan Arduino to drive DIR+ and PUL+. Note: he wrote, “The direction plus pin must be changed whenthe pulse pin is low,” however I tested this, and the direction pin worked correctly no matter whatthe pulse pin's state was. Here is the link: http://forum.arduino.cc/index.php?PHPSESSID=objlfu9417jhvo7006es9l64a0&topic=172537.msg1290522#msg1290522

I used a Raspberry Pi, which uses 3.3v logic (unlike the Arduino, which uses 5v), but it still workedfine for me. Here's how I connected the wires:

HY-DIV268N-5A Connects to:

DIR- RPi GPIO GND

DIR+ RPi GPIO #18

PUL- RPi GPIO GND

PUL+ RPi GPIO #23

EN- RPi GPIO GND

EN+ RPi GPIO #4 or GND

A+ stepper green wire

A- stepper gray wire

B+ stepper red wire

B- stepper yellow wire

DC- GND (black on PC power supply)

DC+ +12v (yellow on PC power supply)

The NEMA 14 stepper motor I tested with was this one, from AdaFruit Industries for $14:http://www.adafruit.com/products/324Of course, much larger steppers will work with this controller. For this test I set the DIP switches for

Page 5: Haydn’s Maker Blog

26/9/2015 Haydn’s Maker Blog

http://www.nlogn.com/blog/ 5/6

0.2A and 1/16 microstepping, which yields 200*16=3200 steps per revolution.

For power, I used an old ATX power supply. By connecting pin #14 (green) to ground (pin #15,black), it turns on when plugged in. Here is the reference I used:http://pinouts.ru/Power/atxpower_pinout.shtml

Here is the Python program I used to test it with:

#!/usr/bin/python## Written by Haydn Huntley ([email protected]) on 08/16/2013.# Feel free to use this code any way you want.# Runs on a Raspberry Pi and drives an HY-DIV268N-5A stepper motor controller.## Note: this program must be run as root in order to use the GPIO calls.

import timeimport RPi.GPIO as GPIO

enablePin = 4 # Note enable is active low!directionPin = 18pulsePin = 23pulseState = 0delay = 0.000001 # 1 microsecond

def setup(): GPIO.setmode(GPIO.BCM)

GPIO.setup(enablePin, GPIO.OUT) GPIO.setup(directionPin, GPIO.OUT) GPIO.setup(pulsePin, GPIO.OUT)

GPIO.output(enablePin, 0) return

def forward(steps): global pulseState print "forward delay=%0.3fms steps=%d" % (delay * 1000, steps) GPIO.output(directionPin, 1) for i in range(steps): pulseState += 1 GPIO.output(pulsePin, pulseState % 2) time.sleep(delay) pulseState %= 2 return

def backwards(steps): global pulseState print "backwards delay=%0.3fms steps=%d" % (delay * 1000, steps) GPIO.output(directionPin, 0) for i in range(steps): pulseState += 1 GPIO.output(pulsePin, pulseState % 2)

Page 6: Haydn’s Maker Blog

26/9/2015 Haydn’s Maker Blog

http://www.nlogn.com/blog/ 6/6

time.sleep(delay) pulseState %= 2 return

def main(): # For the NEMA 14 12v 350mA (#324) stepper motors from AdaFruit: # http://www.adafruit.com/products/324 # Driving it with 12v using a delay of 1 microsecond. revolutions = 3 steps = revolutions * 200 * 16 setup() forward(steps) time.sleep(0.5) backwards(steps) GPIO.cleanup() return

try: main()except: print "something went wrong!" GPIO.cleanup() raiseGPIO.cleanup()exit(0)

It steps the motor at 1 microsecond per step, which is the fastest I could get it to work with Python'stime.sleep() function for the delays. With 1/16 microstepping and 200 steps/revolution, it doesabout 136RPM, which is much better than when I tried this using an LM293D!

For about 20 years I've had a dream of building my own CNC machine for milling wood, plastic,etc., and I'm finally making it happen! :-)

I hope this proves useful to others. If you have any questions, just email me...

[email protected] LastModified:

Sunday, November 24, 12:21pm

Copyright © 1997-2015 Haydn Huntley, all rightsreserved.