Controlling servo with RPi3B+

Hello, I’m a 14-year-old kid from the Czech Republic interested in programming JavaScript and technology with my dad.

We wanted to try driving a servo motor using hardware PWM from my Raspberry Pi 3B + together with Node JS library node-rpio. I will explain here, how I have done it because there is no good summary for me on this topic now on the internet.

The hardware and software used

  • Raspberry PI3b+
  • Servo 6g TBD....
  • Debian TBD
  • Nodejs TBD
  • node-rpio library https://www.npmjs.com/package/rpio

Theory

https://www.js4iot.com/2021/05/30/HardwarePWM-RPIO.html there is explained the theory of PWM( Pulse With Modulation). 

Nice description how the servos work can be found here: https://www.jameco.com/Jameco/workshop/Howitworks/how-servo-motors-work.html

The Servo motor works on pwm signals long 20ms/50Hz. You must set up your pwm in the Raspberry Pi with the rpio library. The hardware pwm module has 2 modes

  • balanced mode - spreads the up and down signasls across the whole cycle Rt uniformly by its smallest units tmin. This is better for audio signals or for LED dimming.

  • mark-space mode - that keeps first part Ut of signal up, and the rest of the cycle Rt down. This perfect for controlling the servos. This is the mode enabled by the rpio library and is the only supported by this library.


The mark-space pwm mode with cycle of R_t seconds, while U_t time the signal is up.


To control the pwm with the rpio library, you have to set up three basic parameters with the following methods

        rpio.pwmSetClockDivider(128);
        rpio.pwmSetRange(this.pin, 3000);
        rpio.pwmSetData(this.pin, 300);


To set the values right, you must know, how your raspberry ticks. RPi 3B+  clock works at 19,2MHz. This is the fastet theoretical speed of the pwm module. 


The frequency of PWM also depends on the clock divider which is set by the pwmSetClockDivider. Let’s denote the value of the divider as D. D can have values values between 2 to 4095.  The final frequency of the pwm module is then the 19,2 Mhz divided by D. That means that our minimal time range for the change from 0 to 1 or vice versa is 

\[ t_{min} = {D \over (19.2 \times 10^6)}\ [s] \]


By the pwmSetRange method we set the width of the pwm cycle. Lets call the value  R and can have values from 1 to 2^32 - 1. The value means how many times the t_min is in one pwm cycle.  The length of the pwm cycle in seconds is then:

\[ R_t = R \times t_{min}\ [s] \]


The pwmSetData sets the width of the part of the cycle when the signal is up. let’s call the value used in the method U. This value defines how many t_min sections are in the up part of the signal.  The value of U can be from 0 to 2^32 - 1

\[ U_t = U \times t_{min}\ [s] \]

As an example, let’s take values form the exaple calls above. The length of the cycle in this example will be

\[ R_t = R \times { D \over {19.2 \times 10^6} } = 3000 \times { 128 \over {19.2 \times 10^6} } = 0.02\ [s] = 20\ [ms] \]

The length of voltage up segment will be

\[ U_t = 300 \times t_{min} = 300 \times {128 \over {19.2 \times 10^6} } = 0.002\ [s] = 2\ [ms] \]


which is exactly the cycle we need for controlling typical servo.


Connecting the RPi 3B+

Connect the raspberry according to the following diagram


TBD diagram.
(Fritzing)







Setting up the Raspberry Pi3B+


Before you will run your program you must setup the RPi to enable the pwm for output.

  • First modify the /boot/config.txt of the raspberry by adding the line
dtoverlay=pwm,pin=12,func=4

and reboot your RPi.


  • For some reason the node-rpi does not enable the pwm automatically. You may enable it using the following script. What exactly is needed is still subject of investigation. However, create the following script and execute it:

$ cat /usr/local/bin/enable_pwm.sh 
#!/bin/sh
cd /sys/class/pwm/pwmchip0
echo 0 > export
echo 10000000 > pwm0/period 
echo 5000000 > pwm0/duty_cycle 
echo 1 > pwm0/enable 


  • Create a systemd service to start the above script after reboot so you don't need to run it every reboot.

# cat /etc/systemd/system/enable_pwm.service 

[Unit]

Description=Enable PWM Module

Wants=dev-pwmchip0.device

After=dev-pwmchip0.device


[Service]

Type=oneshot

ExecStart=/usr/local/bin/enable_pwm.sh

RemainAfterExit=Yes


[Install]

WantedBy=multi-user.target



The simple testing node.js program 


Experiments with the program

TBD - some graphs from the Salea probe, with some settings typical for servor, and some edge settings (maximal values, etc...)

References

  • https://www.npmjs.com/package/rpio the rpio library
  • https://www.js4iot.com/2021/05/30/HardwarePWM-RPIO.html there is explained the theory of PWM( Pulse With Modulation)




Comments

Popular posts from this blog

Turris Omnia and Home Assistant in LXC Container

Turris Omnia Minidlna