In this very short tutorial, I will show you how to controll 2 servos wired on your RaspberryPi. This tutorial is very simple and flexible because of its multiple applications. So, let's start !
If you are bored about reading, I made 2 videos on this subject (in French).
Step 1: Some Stuff
To begin, let me introduce you the stuff we will need to start the project.
We will need :
- 1 Raspberry Pi (we do not care about the model)
- 1 Bluetooth USB dongle (generally they are relatively cheap, get some here)
- 2 Servos (get some here)
- 1 WiiMote (get one here)
- Some wire (you will see why later)
This is the entire stuff we will need to build this project. Now let's make !
Step 2: Preparing the Pi !
When you are ready, plug the power into your Pi and start by cloning this Git repository into your Home by typing this command :
sudo git clone https://github.com/richardghirst/PiBits.git
After go into the Servod directory by typing :
And compile the program :
Next step, we will wire the servos and configure ServoBlaster to make our servos controllables.
Step 3: Wiring the Servos
On the S3003 servos, you should see that there are 3 entries : the red is for the power, black for ground and the white one for the signal.
Powering the servos is very simple, just plug the red wire on a 5V GPIO pin of your Raspberry Pi, after that, plug the black wire on a ground pin.
List of the 5V GPIO pins : 2,4.
List of the ground pins : 9,14,25,30,34,39.
For the signal wire, you have to plug it in basic GPIO pins; I usually plug them in pins 7 (GPIO4) and 11 (GPIO17). The main thing you have to do is remebering which servo is wired to these pins (e.g. pan in on 7 and tilt is on 11) to configure the program.
So, let's configure the program !
Step 4: Configuring ServoBlaster
Once you have wired your servos and you remember the numbers of the pins, start by going into Servod directory :
Here, just type :
You should see somthing like that :
$ sudo ./servod
Board revision: 1
Using hardware: PWM
Using DMA channel: 14
Idle timeout: Disabled
Number of servos: 8
Servo cycle time: 20000us
Pulse increment step size: 10us
Minimum width value: 50 (500us)
Maximum width value: 250 (2500us)
Output levels: Normal
Using P1 pins: 7,11,12,13,15,16,18,22
0 on P1-7 GPIO-4
1 on P1-11 GPIO-17
2 on P1-12 GPIO-18
3 on P1-13 GPIO-21
4 on P1-15 GPIO-22
5 on P1-16 GPIO-23
6 on P1-18 GPIO-24
7 on P1-22 GPIO-25
After that enter this line (pins 7 and 11 are used as examples, you can use yours of course):
This line will specify that 2 signal wire are plugged, one on GPIO7 and the other on GPIO17.
You can now try to make rotate your servos by typing :
sudo echo P1-7=0% > /dev/servoblaster
sudo echo P1-11=0% > /dev/servoblaster
sudo echo P1-7=100% > /dev/servoblaster
sudo echo P1-11=100% > /dev/servoblaster
You should have noticed that the 4 commands entered are usually used to write text into files.
Now, we can setting up Bluetooth to connect our WiiMote to the Pi !
Step 5: Setting Up Bluetooth and WiiMote
Start by plugging your Bluetooth USB dongle into your Pi and reboot it. Next, run :
If your dongle appears, all right, we can continue ! If it not appears, retry...
Now, we need to install the drivers and softwares, type :
sudo apt-get update
sudo apt-get install bluetooth
If the installation ends without APT or DPKG error message, you can continue else, try to solve the problem by typing sudo apt-get -f install and then retry sudo apt-get install bluetooth --fix-missing .
After that check if the Bluetooth service is running :
sudo service bluetooth status
You should see something like :
[ok] bluetooth is running.
If the service not seems to be running, try sudo /etc/init.d/bluetooth start
If all these steps worked correctly, now let's code !
Step 6: Installing the CWiiD Python Library
In order to get informations from the Wiimote through Bluetooth, we have to install a Python library called CWiiD, it is like a "driver" if you want. To install it, it is very simple, juste type :
sudo apt-get install python-cwiid
That's it, now let's code (really) !
Step 7: Coding the Python Script
To get the script just type :
sudo nano ~/wiimote-script.py
Then, copy and paste the piece of code at https://github.com/Cypaubr/WiimoteServos/blob/mast...
Ctrl+O, ENTER and Ctrl+X to save and quit.
Now, it's time to run the script !!!
Step 8: Running the Script
To run the script just type :
sudo python wiimote-script.py
You should now be able to control your servos from your Wiimote.
If the programms throws an erre like
/dev/servoblaster File not found
You just have to relaunch the servod program from Step 4...