Using an IR transmitter with the Raspberry Pi

I am using a 38KHz IR Infrared Transmitter Module for Arduino, this one:

Connecting

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

Testing

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
cd wiringPi
./build

You know the GPIO pin where you connected the OUT, right? So, run:

gpio readall

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
sleep(0.1)
gpio write 5 0
sleep(0.1)
gpio write 5 1
sleep(0.1)
gpio write 5 0
sleep(0.1)
(....)

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.

Using an IR receiver with the Raspberry Pi

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)

Installing

1) sudo apt-get install lirc

2) add on /etc/modules:
lirc_dev
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.

Using the MMA7760 accelerometer with the Raspberry Pi

Connecting

  • 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

Testing

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. 

Using the HC-SR04 ultra sonic sensor with the Raspberry Pi

This one is not very complicated at all.

Connecting

  • 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.

Testing

You run this demo script that gives the distance in cm every X tenths of a second.

 

Using the Tower SG90 servo motor with the Raspberry Pi

Let’s play with the Tower SG 90 servo motor.

Connecting

  • 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…)

Testing

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.

Using the HMC5883L magnetometer/compass on the Raspberry Pi

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)

Connecting compass sensor

Configuration

The HMC5883L compass is easily accessed using the i2c libraries. I followed this tutorial on installing i2c on Raspbian, and let’s recap:

  1. Install i2c:
    sudo apt-get install i2c-tools
  2. edit file /etc/modprobe.d/raspi-blacklist.conf and comment out the line blacklist i2c-bcm2708
  3. edit /etc/modules, and add the lines:
    i2c-bcm2708
    i2c-dev
  4. Allow i2c access from users other than root, by creating the file /etc/udev/rules.d/99-i2c.rules with this line:
    SUBSYSTEM=="i2c-dev", MODE="0666"
  5. Reboot the Pi. When it goes up again, type:
    ls /dev/i2c*
    On my Pi (Model B, Revision 2 version, early 2013) it generates:
    /dev/i2c-0 /dev/i2c-1
  6. Optional: For python, install the smbus python library with:
    apt-get install python-smbus
  7. Install Python 3, can’t hurt, and i2clibraries needs it. Just type sudo apt-get install python3
  8. 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.
  9. Add the quick2wire code. Pull from git:
    git clone https://github.com/quick2wire/quick2wire-python-api.git. On /etc/profile, add: export QUICK2WIRE_API_HOME=/home/pi/quick2wire-python-api
    export PYTHONPATH=$PYTHONPATH:$QUICK2WIRE_API_HOME
  10. Add i2clibraries. Pull from git:git clone https://bitbucket.org/thinkbowl/i2clibraries.git

Testing

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.

Components are here!

My dx.com orders arrived: breadboard, servo motors, sonar sensor, IR transmitter / receiver sensor, accelerometer and compass sensor, wires, LEDs, resistors and transistors.

Raspberry Pi equipment

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

Fitting the Raspberry Pi on the 4×4 trunk

I know there are smaller battery packs around that would fit nicely on the trunk of the 4×4 crawler, but mine is a big 5000 mAh solar charger that neeeded a bigger trunk.

I removed the fenders and extended the trunk, like this:

Fitting a battery on the 4x4 crawler

I got more spare parts, and I even have some locks to make sure that the battery pack doesn’t dance while on an off-road course. The IR sensor is still unblocked.

Fitting a battery on the 4x4 crawler

The 4×4 Crawler car is now assembled.

Lego Technic 9398 4x4 Crawler with a Raspberry Pi on top

This Lego car is amazing. Beautifully crafted, robust, beautiful, and lots of fun! Can’t ask for a more perfect choice for this project.

I’m planning on leaving the Raspberry Pi on the rooftop, the battery on the trunk, and the bread board either on the hood, or on the cockpit. The camera goes into the front bumper, and the servo with the sonar will be attached to the hood with some Lego bars — it is going to oscillate too much, so it needs a sturdy fixation. Other attachments will be made with velcro — the compass and accelerometer can co on the top of the Pi box.

Now, it’s time to wait for the electronics to arrive, maybe next week. First steps:
– investigate the servo bought, how to attach it to the car, and how to control it with the Pi.
– investigate Lego’s IR protocol, setup the IR transmitter, and try to control the car with some python controls.

The Rover Car Mk II plan

So, time to buy the new Rover Car. On the shopping list:

– A Lego Technic 9398 4×4 Crawler
– Raspberry Pi bread board, jumpers, cobbler kits, resistors, LEDs, etc.
– Sensors: an ultrasonic sensor, an IR transmitter, accelerometer, compass.

Where does the Raspberry Pi fits in it? And what it the autonomous plan?

The Lego car engines are controlled by an IR receiver. This page has a thorough explanation of the Lego IR receiver contacts and the motor contacts, and how they operate. Basically, the IR receiver channels the 9V from the battery pack into the C1 and C2 channels of the engines, both servo and DC engines, thus making them rotate. 

The first idea I had was to put the Raspberry Pi between the IR receiver and the engines. The advantage were:

 – I could have a direct control of the engines, just like with the Mk. I Rover car.

yet, the disadvantages were higher: 

 – I had to cut the motor wires so I could add them into the bread board. I could order some extension wires from the Lego shop, nonetheless. 

– We are dealing with 9 V here, and any mistake can cause me to ruin my Raspberry Pi, and I don’t want that.

– No idea on how to still make both manual and automatic controls work, that is, making the Raspberry Pi to contol the car, while still having manual control to override it.

There is a better solution: instead of cutting wires and using the bread board for engine control, why can’t I just use an IR transmitter? 

The advantages are: 

– No wire cutting. Raspberry Pi sends IR signals to control the engines.

– No need to modify the 4×4 crawler car. Everything stays there. 

– Both the Lego controller and the Raspberry Pi controller can send IR signals, so they can both control the car at the same time. 

– No risk on ruining the Raspberry Pi. Bread board stuff is only for the sensors, who use voltage that the Pi can handle, no need for 9 V. 

The only disadvantage, so far, and not a big one, is that I have to teach the Raspberry Pi to speak “Legolese”, but since Lego kindly published the RC protocol, anyone can reproduce the IR codes.

The next thing is the sensors: how to detect the obstacles around the car? Th first idea was to buy 8 ultrasonic sensors, and placing them on the corners of the car, and on the sides. That idea was scrapped immediately, when I though that a cheap servo could do the trick.

This boebot shows what I have in my mind:

If I attach the ultrasonic sensor to a servo motor, and if I can control its angle, then I can do several measures per second, on a 180º range. That is more than enough to detect big obstacles such as walls or boxes, and it is fine for now.

This is good enough to make sure that the car can drive autonomously without hitting walls. Some Python code can ensure that he knows how to turn and reverse, if such obstacles are found. Yet, not good enough if we want the car to go from point A to point B.

Now, that is the core of Artificial Intelligence research, and that is basically given by whatever Python code I can put in the Raspberry Pi. So, let’s leave it there; in terms of hardware, I believe that an accelerometer and a compass are important, in order to provide a certain awareness of the whereabouts of the car (a GPS receiver wouldn’t be much help on a car that has a speed of 50 cm per second, operating inside buildings). If the car knows his speed and bearings, it can calculate its position from a reference point; given a destination point, he should at least know the bearings where he should be heading.

Now, it’s time to wait for the sensors and Pi accessories to arrive. The Lego car is here, and already assembled, waiting for some Pi sweetness.