I am using a 38KHz IR Infrared Transmitter Module for Arduino, this one:
I connected exactly like the IR receiver, as the circuit boards are quite similar. In a nutshell:
- Left – GND – connect to Pi’s pin 6
- Middle – VCC – connect to Pi’s pin 1 (3.3V – I didn’t see it working on 5V)
- Right – OUT – connect to any Pi’s GPIO pin
To see it working, I had my IR receiver working, and I ran
mode2 -d /dev/lirc0
to see the IR receiver output.
I installed the WiringPi library, as it has a very simple way to play with GPIO pins.
So, run on your /home/pi:
git clone git://git.drogon.net/wiringPi
You know the GPIO pin where you connected the OUT, right? So, run:
and check what is the WiringPi number got that pin. For example, GPIO 5 (pin 24) is WiringPi number 5.
Check if that pin is on IN or OUT, that is, if it is configured to read or to write. Since we are going to write data, we set it to output with:
gpio mode 5 out
Now I created a bash script with several lines as:
gpio write 5 1
gpio write 5 0
gpio write 5 1
gpio write 5 0
and everytime I run the script, I can see that the IR receiver outputs a bunch of lines, so yes, it is sending IR messages.
I have a TSOP1838 IR Infrared 37.9Khz Receiver (18m-Range / 2.7~5.5V) that I bought on dx.com.
It comes in a board, and it was a nightmare to understand which pin goes to… I actually tried it wrong and the sensor heated a lot, but luckily it wasn’t ruined.
Furiously googling for any answer, I saw this picture that pointed me into the right direction.
So, with the board facing you with the sensor and the pins pointing down, it is:
- Left – GND – connect to Pi’s pin 6
- Middle – VCC – connect to Pi’s pin 1 or 2 (works on both 3.3V or 5V, but read further down on this)
- Right – OUT – connect to any Pi’s GPIO pin (in the picture, it is on the pin 12, the default one for lirc)
1) sudo apt-get install lirc
2) add on /etc/modules:
lirc_rpi gpio_in_pin=18 gpio_out_pin=24
You do not need to set gpio_in_pin and/or gpio_out_pin.
Note that those are GPIO numbers, so they actually match physical Pi’s pins nrs 12 and 18, respectively.
For this IR receiver, the one that matters is the gpio_in_pin.
3) reboot helps. Then do sudo /etc/init.d/lircd stop
Make sure that the daemon is stopped, we do not need it.
4) mode2 -d /dev/lirc0
That will debug what the sensor is seeing. You should see a lot of spaces and pulses if you use your TV’s remote controller and press random buttons into the IR receiver.
I observed that the IR receiver captures a lot of noise signal when it is working on 3.3v. Powering it with 5V eliminates most, but not all of the noise.
- VCC – this is the +3.3V power supply connection, connect to Pi’s pin 1 (3.3V)
- GND – this is the power supply ground, connect GND to Pi’s pin 6 (ground)
- SCL – this is the I2C clock signal, connect SCL to Pi’s pin 5 (probably you can use other pin)
- SDA – this is the I2C data signal, connect SDA to Pi’s pin 3 (probably you can use other pin)
- INT – this is the I2C interrupt signal – we won’t use this for now
Unfortunately, I haven’t yet made it work properly. I am having the same problem as this guy or even this guy, where all the axis read the same value.
Yet, I compiled the C code for the MMA7660 given by this guy here, and it works fine. Nonetheless, I want to use only Python on my project, and since the accelerometer is not a very important sensor for now, I will leave it on the side for now.
This one is not very complicated at all.
- VCC pin goes to Pi’s pin 2 (5V power)
- GND pin goes to Pi’s pin 6 (ground)
- Trigger pin goes to Pi’s pin 5 (but can be configured to use other pin)
- Echo pin goes to Pi’s pin 3 (but can be configured to use other pin)
You can see the connections on the picture below.
You run this demo script that gives the distance in cm every X tenths of a second.
Let’s play with the Tower SG 90 servo motor.
- Brown wire is a ground wire
- Orange wire is a power wire
- Yellow wire is a signal wire.
Connect both brown & orange wires to an external battery (4.5v or 6V are fine).
If you use the Raspberry Pi to power the servo motor with the 5V pin, it will crash the Pi, as it does not have enough power once the servo starts moving too much.
Connect the signal wire to Pi’s pin 12 (GPIO 18, the PWM pin, or the wiringPi pin 1).
Damn, that thing is light!
So, I attached the sonar to the servo, and then connected the servo motor to a 4.5V battery pack I “created” (not perfect, but a squeeze and a LED signal helps…)
I used a modified version of this demo code to send loop signals to the servo motor, ranging from 1 to 12, which game me a 180º coverage.
This means that I have a 15º resolution for each sonar capture, which I believe is quite enough. The trick here is to trigger the sonar when the servo is stationary, proceed to the next angle, stop, trigger the sonar, all in the shortest time possible.
From what we can see, on the video, this servo takes approximately 1 second to do the 180º (with 4.5V), so I assume 2 seconds with sonar trigger i a good number. Fine for me.
Let’s work with the HMC5883L magnetometer/compass.
Connecting the compass sensor
VCC – power 3.3v – Pi’s pin 1 (red cable)
GND – ground – Pi’s pin 6 (black cable)
SCL – serial clock – Pi’s pin 5 (orange cable)
SDA – serial data – Pi’s pin 3 (brown cable)
The HMC5883L compass is easily accessed using the i2c libraries. I followed this tutorial on installing i2c on Raspbian, and let’s recap:
- Install i2c:
sudo apt-get install i2c-tools
- edit file
/etc/modprobe.d/raspi-blacklist.conf and comment out the line
/etc/modules, and add the lines:
- Allow i2c access from users other than root, by creating the file
/etc/udev/rules.d/99-i2c.rules with this line:
- Reboot the Pi. When it goes up again, type:
On my Pi (Model B, Revision 2 version, early 2013) it generates:
- Optional: For python, install the smbus python library with:
apt-get install python-smbus
- Install Python 3, can’t hurt, and i2clibraries needs it. Just type
sudo apt-get install python3
- Test if the compass is detected, by typing:
i2cdetect -y 1 (for Revision 1 Pis, replace 1 with 0).
Replace with 0 for Revision 1 Raspberry Pis and with 1 for Revision 2 boards. This is the output:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Tip: if you don’t see it, it’s because you haven’t welded the pins to the sensor. Just press with your finger. Or weld it.
- Add the quick2wire code. Pull from git:
git clone https://github.com/quick2wire/quick2wire-python-api.git. On
- Add i2clibraries. Pull from git:
git clone https://bitbucket.org/thinkbowl/i2clibraries.git
I used this demo code to check the sensor. First, I had to visit http://magnetic-declination.com/ and fix the magnetic declination on the script. Mine was:
You are here*OSLO OSLO
Latitude: 59° 54' 45.8" N
Longitude: 10° 44' 45.9" E
Magnetic declination: 2° 20' EAST
Declination is POSITIVE
Inclination: 72° 43'
Magnetic field strength: 51056.8 nT
The script will output something like:
Axis X: -113.16
Axis Y: -396.52
Axis Z: 368.92
Declination: 2° 20′
Heading: 256° 39′
The heading is what we want. Yup, now I have bearings on the Pi.
My dx.com orders arrived: breadboard, servo motors, sonar sensor, IR transmitter / receiver sensor, accelerometer and compass sensor, wires, LEDs, resistors and transistors.
Here is the complete list:
- HC-SR04 ultra sonic sensor
- HMC-5883L compass
- MMA-7660 accelerometer
- Tower SG-90 servo motor
- TSOP1838 37.9Khz IR receiver (based on vb1838b)
- KY-005 board for a generic 38KHz IR LED transmitter