With the new jumper leads from eBay I wired it all up again. For the power, the motor controller board has 4 pins and I don’t know why. Two of them are bridged by a jumper and by the other two there is a “-” and a “+” and “5-12V” underneath. I wired the 5V pin of the Pi (P2) to the one labelled “+” and the ground pin of the Pi (P1) to the “-“. Seems to work, but I don’t know if I just got lucky or whether it doesn’t matter which way round you put them.
For the controller lines I wired P18, P22, P24 and P26 to IN1, IN2, IN3 and IN4 respectively. That done, I turned it on and tried some software.
I think I tried the Python code on Matt’s blog first but somehow it didn’t quite work - I don’t remember now. What was great though was that although the motor didn’t seem to turn the LEDs came on so I had got something happening! After a bit of puzzling I realised that the LEDs are wired to the four control lines of the motor so they are very useful in diagnosing what is happening and checking you’ve got the right control sequence.
I spent some time Googling (finding Patrick’s blog), trying to understand the motor’s datasheet, and combining Matt and Patrick’s Python code to make my own version that works for me:
Lines 12-15 are just dealing with command line arguments: you can tell it how many times to turn round with -revs and how fast to go with -rpm (revolutions per minute). I found that 10rpm was about as fast as it can go.
Lines 23-38 set the pins up using the pin numbers from the Raspberry Pi’s circuit board (starts 1 bottom left, 2 top left and goes up from there). It sets them to be outputs and sets them to zero (0V I assume).
Lines 41-57 actually encode the sequence of ons and offs given in the motor’s datasheet and loops round it the right number of times. Finally, it sets all the pins to zero again. I found it made the motor turn anti-clockwise, though the datasheet says the sequence is for clockwise rotation - perhaps it is because I plugged the 5V and ground pins in the wrong way round?
I called it spin.py. To run it you have to type sudo python spin.py because you have to have root permissions to access the GPIO port.
Once this worked I started trying to do something more sophisticated but quickly realised that I needed to make a class to hold the state of the motor. More on that next time…
Comments
Comments powered by Disqus