Create Project Need help?
Show more (3)
  • Labs project

Raspbery Pi - Control a stepper motor with a rotary encoder

Status: Proposal
June 4, 2015

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.

Wiringpi & rotary-servo.php
You need to first install apache2 & php5-dev before installing wiringpi.

  • sudo apt-get install apache2 php5 php5-dev
Now use git to install wiringpi
  1. git clone
  2. cd WiringPi-PHP
  3. git submodule update -init
  4. sudo ./
  5. sudo ./
  6. cp wiringpi.php /var/www/
Now wiringpi is installed in your Raspi, also it got copied to the /var/www directory, but to get it inside PHP we need to load it in the ini file.
Open a wiringpi.ini file in /etc/php5/conf.d/ and write the following lines in it. Save it and restart apache server.
  1. sudo nano /etc/php5/conf.d/wiringpi.ini
  3. wiringpi.pinmaptype=PINS
  4. save the file and restart apache .
  5. /etc/init.d/apache2 restart
Run the following command to check whether the wiringpi module is loaded or not.
  • php -m | grep wiringpi
Alternatively you can make an info.php file in the server document root directory (normally /var/www ) and then load it in the browser to check it.
  1. info.php
  3. phpinfo();
  4. ?>
Run it in the browser
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
To make it running every time the Raspberry Pi computer boots...
In a terminal run
  • $>sudo nano /etc/rc.local
//then before the exit command anywhere in between write
  • 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

Read the full post
Show less

Loading comments...