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

Embed Size (px)

Citation preview

  • 8/13/2019 Raspberry Pi: Access a Motor with Gertboard and node.js

    1/5

    handson.dmt.fh-joanneum.at

    Carelse, Pauger, Pilz 1

    RASPBERRY PI

    Workpackage: Access a Motor with Gertboard and node.js

    Description: This worksheet is about how to start, stop and regulate a motorwhich is connected via the Gertboard to the Raspberry Pi.

    Difficulty (1-10): 8

    Overview: Prepare Gertboard

    node.js Code

    Test your Code

    Circuit Schematic

    Useful Resources

    Requirements: Hardware:Raspberry PI and Standard EquipmentGertboardDrive Motor Rb 35Akku-Powerpack 12 V / 2400 mAh

    Software:

    Node.js

  • 8/13/2019 Raspberry Pi: Access a Motor with Gertboard and node.js

    2/5

    Instructions handson.dmt.fh-joanneum.at

    Carelse, Pauger, Pilz 2

    DIGITAL INPUTS AND OUTPUTS

    This worksheet is about how to regulate a motor which is connected via the Gertboard to theRaspberry Pi.

    To regulate the speed we use PWM. The concept behind is explained very well in theGertboard user manual.

    The main point of this worksheet is to use node.js for regulating the motor.

    Instructions

    Prepare GertboardConnect 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 CodeThe first important thing is, that you have to require wiring-pi and sleep.

    varwiringpi =require('wiring-pi');varsleep =require('sleep');

    We use two pins of the Raspberry Pi to control the motor. Pin 17 for direction of motor andpin 18 for PWM.

    As second step you have to setup the pins.wiringpi.wiringPiSetupGpio() // Initialise wiringpi GPIOwiringpi.pinMode(18,2) // Set up GPIO 18 to PWM modewiringpi.pinMode(17,1) // GPIO 17 to outputwiringpi.digitalWrite(17,0) // port 17 off for rotation one waywiringpi.pwmWrite(18,0) // set pwm to zero initiallyWe provide a function for save exit, which resets all ports.

    functionreset_ports(){ // resets the ports for a safe exitwiringpi.pwmWrite(18,0) // set pwm to zerowiringpi.digitalWrite(18,0) // ports 17 & 18 offwiringpi.digitalWrite(17,0)wiringpi.pinMode(17,0) // set ports back to input mode

    wiringpi.pinMode(18,0)}Then there is another function which regulates the speed of the motor. We pass start_pwmand 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).

    functionloop(start_pwm,stop_pwm,faster){

    //go from start_pwm to stop_pwmif(faster){

    for(vari =start_pwm;i

  • 8/13/2019 Raspberry Pi: Access a Motor with Gertboard and node.js

    3/5

    Instructions handson.dmt.fh-joanneum.at

    Carelse, Pauger, Pilz 3

    }//go from stop_pwm to start pwmelse{

    for(vari =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 themin 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 preventyour motor from going on if you quit your application. In the end the last part of your codeshould look like this:

    try{//increasing speed lefthandloop(200,1200,true)//decreasing speed lefthandloop(200,1200,false)

    wiringpi.digitalWrite(17,1) // port 17 ON for opposite rotation

    //increasing speed righthandloop(100,800,false);//decreasing speed righthandloop(100,800,true);

    }catch(exception){ // trap a CTRL+C keyboard interrupt

    console.log(exception);reset_ports() // reset ports on interrupt

    }finally{

    reset_ports() // reset ports on normal exit

    }Test your CodeAs 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.jscommand.

  • 8/13/2019 Raspberry Pi: Access a Motor with Gertboard and node.js

    4/5

    Circuit Schematic handson.dmt.fh-joanneum.at

    Carelse, Pauger, Pilz 4

    Circuit Schematic

    PICTURE 1: GERTBOARD

    PICTURE 2: INSTALLATION

  • 8/13/2019 Raspberry Pi: Access a Motor with Gertboard and node.js

    5/5

    Useful Resources handson.dmt.fh-joanneum.at

    Carelse, Pauger, Pilz 5

    Useful Resources

    Gertboard and WiringPihttp://wiringpi.com/examples/gertboard-and-wiringpi/

    Installation of WiringPi

    https://github.com/Soarez/node-wiring-piPower Pack:

    http://www.conrad.at/ce/de/product/206974/Conrad-energy-NiMH-Sub-C-Akku-Powerpack-12-V-2400-mAh-Bauform-Side-by-SideStecksystem-T-Buchse?ref=searchDetail

    Motor:http://files.voelkner.de/225000-249999/227552-da-01-ml-GETRIEBEMOTOR_RB35_1zu50_de_en.pdf