Raspberry Pi: Access a Motor with Gertboard and node.js | Raspberry Pi | Digital Electronics



Workpackage: Description: Difficulty (1-10): Overview: Access a Motor with Gertboard and node.js This worksheet is about how to start, stop and regulate a motor which is connected via the Gertboard to the Raspberry Pi. 8 Prepare Gertboard node.js Code Test your Code Circuit Schematic Useful Resources Hardware: Raspberry PI and Standard Equipment Gertboard Drive Motor Rb 35 Akku-Powerpack 12 V / 2400 mAh Software: Node.js


© Carelse, Pauger, Pilz




This worksheet is about how to regulate a motor which is connected via the Gertboard to the Raspberry Pi. To regulate the speed we use PWM. The concept behind is explained very well in the Gertboard user manual. The main point of this worksheet is to use node.js for regulating the motor.

Prepare Gertboard
Connect the Gertboard to the Raspberry Pi, be careful that the Pin 1 on the raspberry (left upper corner) is connected to the Pin 1 on the Gertboard (marked with a square). Connect the following with jumpers as shown in picture 1: - GP18 to MOTA - GP17 to MOTB - motor leads connect to MOTA and MOTB - power supply for the motor connect to MOT+ and ground

node.js Code
The first important thing is, that you have to require „wiring-pi” and “sleep”.
var wiringpi = require('wiring-pi'); var sleep = require('sleep');

We use two pins of the Raspberry Pi to control the motor. Pin 17 for direction of motor and pin 18 for PWM. As second step you have to setup the pins.
wiringpi.wiringPiSetupGpio() wiringpi.pinMode(18,2) wiringpi.pinMode(17,1) wiringpi.digitalWrite(17, 0) wiringpi.pwmWrite(18,0) function reset_ports(){ wiringpi.pwmWrite(18,0) wiringpi.digitalWrite(18, 0) wiringpi.digitalWrite(17, 0) wiringpi.pinMode(17,0) wiringpi.pinMode(18,0) } // // // // // Initialise wiringpi GPIO Set up GPIO 18 to PWM mode GPIO 17 to output port 17 off for rotation one way set pwm to zero initially

We provide a function for save exit, which resets all ports.
// resets the ports for a safe exit // set pwm to zero // ports 17 & 18 off // set ports back to input mode

Then there is another function which regulates the speed of the motor. We pass start_pwm and stop_pwm to this function. We need two directions (variable faster) of the loop to enable two directions of the motor (slow to fast/fast to slow).
function loop(start_pwm, stop_pwm, faster) { //go from start_pwm to stop_pwm if (faster) { for (var i = start_pwm; i<stop_pwm; i++) { wiringpi.pwmWrite(18, i) sleep.usleep(13000); }
© Carelse, Pauger, Pilz 2




} //go from stop_pwm to start pwm else { for (var i = stop_pwm; i>start_pwm; i--) { wiringpi.pwmWrite(18, i) sleep.usleep(13000); } }

Now we can call our function with different values. Try out different values and find out the min and max of your own motor. To increase speed left-hand (anticlockwise) call function with true:
loop(200, 1200, true)

To decrease speed left-hand (anticlockwise) call function with false:
loop(200, 1200, false)

To turn the motor in the other direction (right-hand or clockwise):
wiringpi.digitalWrite(17, 1)

Again to increase call function with true and to decrease call function with false:
loop(100, 800, false) loop(100, 800, true)

You should put the function calls into a try statement, to catch if errors occur and to prevent your motor from going on if you quit your application. In the end the last part of your code should look like this:
try{ //increasing speed lefthand loop(200, 1200, true) //decreasing speed lefthand loop(200, 1200, false) wiringpi.digitalWrite(17, 1) //increasing speed righthand loop(100, 800, false); //decreasing speed righthand loop(100, 800, true); // trap a CTRL+C keyboard interrupt // reset ports on interrupt // reset ports on normal exit

// port 17 ON for opposite rotation

} catch(exception){ console.log(exception); reset_ports() } finally{ reset_ports() }

Test your Code
As always be sure that you have connected everything as shown in picture 1. Then you can run your code by using the (sudo) nano programname.js command.

© Carelse, Pauger, Pilz


Circuit Schematic


Circuit Schematic



© Carelse, Pauger, Pilz


Useful Resources


Useful Resources
Gertboard and WiringPi http://wiringpi.com/examples/gertboard-and-wiringpi/ Installation of WiringPi https://github.com/Soarez/node-wiring-pi Power Pack: http://www.conrad.at/ce/de/product/206974/Conrad-energy-NiMH-Sub-C-Akku-Powerpack12-V-2400-mAh-Bauform-Side-by-SideStecksystem-T-Buchse?ref=searchDetail Motor: http://files.voelkner.de/225000-249999/227552-da-01-mlGETRIEBEMOTOR_RB35_1zu50_de_en.pdf

© Carelse, Pauger, Pilz


Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.