Python, Raspberry Pi

Raspberry Pi & Arduino: a laser pointer communication and a LDR voltage sigmoid

So I finally got some more time to play with my Raspberry Pi GPIOs and Arduino, this post will explain how to use a LDR (Photoresistor, Light Dependent Resistor) on the Raspberry Pi to detect a laser light emitted by an Arduino.

Plugging the laser module on Arduino

Isn’t easy to plug a module when you have no datasheet or some wiring schemes, the chances are that you’ll probably burn it if you provide a current greater than the supported. The module I have was bought on the DX.com and it came with 3 pins, “-” mark, “S” mark and a middle pin without any mark, I assumed that the “S” meant “Signal” and the “-” is the GND pin so I started plugging it on the Arduino pin 14 with a 440 ohm resistor and later I discovered that a 100 ohm resistor was giving the best bright laser without burning it; my advice is to not connect directly any of these modules without a resistor to limit the current, otherwise you’ll end up burning the diode. Here is the photo of the laser diode in front of the LDR:

LDR and Laser (click to enlarge)

Here is the wiring scheme to plug the laser on the Arduino:

Wiring scheme for the laser

And here is the scheme on the breadboard of the Arduino with the 100-ohm resistor:

The “S” is on the pin 12.

In order to test the laser, I just uploaded this program to Arduino, it just blinks the laser module:

int laserPin = 12;

void setup()
{
    pinMode(laserPin, OUTPUT);
    digitalWrite(laserPin, HIGH);
}

void loop()
{
    digitalWrite(laserPin, HIGH);
    delay(2000);
    digitalWrite(laserPin, LOW);
    delay(2000);
}

Look Ma, no ADC !

Since Raspberry Pi doesn’t come with an Analog-to-digital converter (ADC) like Arduino, connecting a LDR to its GPIOs pins is a little tricky (when you don’t have a Gertboard). The traditional approach is to use a capacitor and then time its charging according to the LDR resistance, this isn’t a precise measure, but it works well for most applications. What I’m going to do here is another approach, since I just want to measure if there is a laser light present or not (I’m not interested in the light energy), I don’t need to time the capacitor charging.

The first step was to measure the resistance that the LDR creates when the laser light is present, to do that I turned the pin 12 to HIGH on the Arduino and then measured the resistance on the LDR with the multimeter:

The laser emitting on the LDR

And the measure was roughly 200 ohms when the laser wasn’t pointing to the LDR (even with the daylight) the resistance was around the 2k ohm. Now we know that we have to build a circuit that is will use the interval of 0 ohms to 400 ohms (a good margin) as the threshold to activate/deactivate the GPIO pin of the Raspberry Pi.

The challenge now was how to create a circuit that would set the GPIO pin of the Raspberry Pi according to a defined threshold. It turns out that with the help of Oli Graser (see more about the circuit here), I discovered that you can create a voltage sigmoid with a voltage divider circuit outputting to a general transistor, and this is really awesome, can you imagine something like a logistic regression built in hardware ? Absolutely interesting.

Voltage divider and the “sigmoid” circuit

And here is the schematics of the voltage divider using the transistor as control of the GPIO pin:

Circuit

The first part of this circuit is the LDR and the R2 resistor, together they work as a voltage divider, when the laser is off, the LDR resistance is very high and then the voltage that goes to the base of the transistor Q1 (I used a BC547B) is very low, making the GPIO pin 4 state to go HIGH, when the laser is on, the LDR resistance is very low and the voltage going to the base of the transistor is enough to activate the circuit and makes the GPIO ping 4 state to go LOW since the current will flow to the path of less resistance, which is to the GND.

Below you can see the graph of the simulation I’ve done using the amazing Circuit Lab service:

The X-axis is the LDR resistance and the Y-axis is the GPIO pin 4 voltage.

You can see in this graph is very similar to a sigmoid function, I’ve chosen the threshold by setting the voltage divider bottom transistor R2 to 100 ohms, you can adjust this as you wish to make, for LDR 500 ohms threshold I calculated 500 ohm * (0.7v / 0.3v) = 106 ohms (close to the 100 ohm resistor I used), the higher the bottom resistor value you use, the higher will be the center of the sigmoid.

Here is the circuit already built on the breadboard:

The circuit on breadboard (click to enlarge)

And a zoom on the voltage divider part:

Voltage divider part of the circuit

And a zoom on the transistor part:

Transistor part of the circuit

And finally, the complete mount of the system:

Complete setup (click to enlarge)

I used tape to fix the components on the table (there, I fixed it !).

Reading the GPIO state from Raspberry Pi using Python

To setup (as INPUT) and read the GPIO pin 4 of the Raspberry Pi (you can see the pins explanation here) I used the RPi.GPIO Python module, to install it just use “sudo pip install RPi.GPIO”, but you’ll need to have the package “python-dev” installed first if you’re using Raspbian distro. Here is the code to read and show the GPIO pin 4 value:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

while True:
input_value = GPIO.input(4)
print "Input Value (PIN 4):", input_value
time.sleep(0.1)

And that is it, I hope you liked it and learned something new like I have learned to do this =) the next step will be to check how far I can transmit data using the laser pointer, I discovered that I can read using Python without any problems a change frequency of 2 milliseconds on the Raspberry Pi, this should be better using C code, I still need to implement the transmission protocol.