Sunday, February 11, 2018

Raspberry Pi Servo Motor Controller

DC Servo Controller Shield of Raspberry Pi

I found Adafruit motor controller shield for Raspberry Pi.
This shield supports up to 16 channel PWM for DC servo motor.
I do not know if I need all 16 channels, but it seems like very easy to use and can work for many servos and other PWM applications like LEDs.
Anyway, I decided to get one and play with it.

Setup
The setup is so easy, especially with all the instructions explained in the product page here.
I pretty much followed this page for all the setups.
For the servo motor, I used tilt and pan servo system with SG-90 from TowerPro.
The spec of this servo is:

  • Size : 23x11x29 mm
  • Voltage : 3V to 6V DC
  • Weight: 9g / 0.32oz
  • Speed : 0.12 sec/60 (at 4.8V)
  • Torque : 1.6 kg-cm

1. Assembly
When I received the package, all the header pins were not soldered, so I need to solder those header pins.
I did not mount capacitor this time but the product page explains about the "good to start" values which is n * 100uF where n is the number of servos if you are driving many servo or a lot of current.

2. Power
I used DC 5V 2A  power adapter and as I mentioned above, I did not mount any capacitors on the board.


2. Connection

Servo has 3 wires which is power, ground, and control signal (PWM).
In my case, orange, red, and brown, and those are signal, power, ground, respectively.
The board has silk screen with S, V+, and G and as you can probably guess this means Signal, Power, and Ground, respectively.
I connected the pan and tilt servo wires to channel 0 and 1 of my shield board.


                  

3. Testing
This shield board uses I2C for the communication. 
First of all, I would recommend to check if raspberry pi can recognize the I2C slave device on the shield board.

If I2C is not enabled on your raspberry pi, you can follow this tutorial. 
For this test, I used "i2c-tools" package and here is how to install and use it.

Install python-smbus and i2c-tools:
sudo apt-get install python-smbus 
$ sudo apt-get install i2c-tools
Run i2c test command:
$ sudo i2cdetect -y 1

If you see 0x40 (default I2C address of the shield board) in the output table, that means you are good to go.


Python code for testing
I used python to control the servo since there is very useful python code examples from Adafruit.
You can find all the information here.
This is my python example code using my servo system.
    *** Depending on the python version, you may get different result and my python version is 2.7.13.

Example:
#!/usr/bin/python

from Adafruit_PWM_Servo_Driver import PWM
import time

pwm = PWM(0x40)
#pwm = PWM(0x40, debug=True)

servo0Mid = 360
servo1Mid = 450

pwm.setPWMFreq(60)
pwm.setPWM(1, 0, servo1Mid)
time.sleep(1)
pwm.setPWM(0, 0, servo0Mid)
time.sleep(1)

Crate a python script file as below and copy&paste the above code.
nano servo-test.py
Run the python script.
python servo-test.py
You will see the servo will move and stay at around the center position.
Done!

No comments:

Post a Comment