Raspbery Pi - Control a stepper motor with a rotary encoder
Problem of speed control
Problem of speed control:
The other day we had a big problem. The Saddle feeder's speed control was done by a hydraulic device which consists of a wheel having a complete 360 degree rotation representing from zero speed to maximum speed. The operator climbs the ladder set the wheel, lock it and then came back to the other platform and run the feeder for feeding coal to the bunker.
Till now the Saddle feeders used to run at highest speed to deliver maximum coal to the bunkers for producing electricity but ever since the coal started arriving from Indonesia the operator need to modulate the speed of these Saddle feeders as the demand from boiler is reduced due to very high calorific value of these imported coal. Therefore, he needs to climb the ladder many times in a day to readjust the speed. He demands an easy solution to these difficult speed setting job.
Raspberry Pi in action:
Well, here is the solution based on Raspberry Pi using a rotary encoder and driving a powerful stepper motor directly mounted on the hydraulic disc. The rotary encoder is an easy, efficient yet cheap device for creating directional rotation in digital format. A typical rotary encoder has 3 or more leads, while rotating the knob the common lead goes on touching the other leads sequentially. The common lead goes to the ground. Thus by just following which lead becomes ground after what, a direction of rotation is established and a counter is incremented at the same time as a measurement of rotation along that direction.
So measurement is the first step towards solution. After measuring the small movements of the rotary encoder we feed that reading in the stepper motor for making the same amount of stepped movement to rotate the hydraulic disc and the speed changes. So easy as if pushing a hill by a small twisting of the rotary encoder. The same technique can be employed for making fine tune of the alignment of telescope focus mechanism, direction of a disc antenna to readjust satellite drifting, mast of a crane for placing it on the load and many more.
That's a 3 wire rotary encoder. The cost is barely Rs:200 from mail order shops in India. The red wire is the common lead while the blue wires are the two points – A & B. see schematic for detail.
Well, here's the PHP program for the entire set up. The rotary encoder is connected to the physical pin 12 & 15 while the central pin goes to the ground pin no 6. The stepper motor is connected to the pins 16,18,24,26 (GPIO pin 7, 8, 23, 24). The chip used is L293 or L298 dual H bridge, the motor enable pin is left disconnected as for stepper motors they are not required. The common wire of steeper will be connected to the +5 volts (pin no-2 ) of the Raspi.
Software:
Wiringpi & rotary-servo.php
You need to first install apache2 & php5-dev before installing wiringpi.
- sudo apt-get install apache2 php5 php5-dev
- git clone https://github.com/WiringPi/WiringPi-PHP.gt
- cd WiringPi-PHP
- git submodule update -init
- sudo ./build.sh
- sudo ./install.sh
- cp wiringpi.php /var/www/
Open a wiringpi.ini file in /etc/php5/conf.d/ and write the following lines in it. Save it and restart apache server.
- sudo nano /etc/php5/conf.d/wiringpi.ini
- extension=wiringpi.so
- wiringpi.pinmaptype=PINS
- save the file and restart apache .
- /etc/init.d/apache2 restart
- php -m | grep wiringpi
- info.php
- phpinfo();
- ?>
http://localhost/info.php or http:// (whatever ip address)/info.php from remote and lookout for the wiringpi module in it.
In most likelihood you have now installed the wiringpi for PHP. Now lets get to our actual work.
Now in a termical run rotary-servo.php by
- $> sudo php rotary-servo.php
In a terminal run
- $>sudo nano /etc/rc.local
- sudo php /home/pi/wherever-your-program-is-located/rotary-servo.php &
save nano by ctrl+x and then press 'y'.
Now reboot your Raspberry Pi computer and the program will start. Rotate the encoder slowly and the stepper motor will rotate accordingly.
The article appeared in EFY- India July'15
S. Bera
vindhyanagar.
Discussion (1 comment)