LDR Wireless Sensor with PICAXE ERF

August 24, 2013

Following on from http://drumcoder.co.uk/blog/2013/aug/22/picaxe-urferf-hello-world-ubuntu/ I wanted to transmit something that was gathered from a sensor, rather than just static text. All I have available at the moment is a light dependent resistor (LDR), so I endeavoured to use that to send continuous telemetry on how light/dark it was through to a PC running ubuntu.

Sensor

I connected an LDR to my PICAXE 8M2+ using the circuit at http://www.picaxe.com/Circuit-Creator/Sensors/LDR-Light-Dependent-Resistor/. The output was connected to pin C.2 of my PICAXE.

Program

The following PICAXE program was used, with an LED on C.1:

main:
    setfreq m8
    high C.1
    readadc10 C.2, w1
    bintoascii w1,b4,b5,b6
    serout C.4,N9600_8,("LIGHT",b4,b5,b6, "_")
    pause 1000
    low C.1
    pause 1000
    goto main

The key parts of this to note are:

  • readadc10 C.2, w1 - read the value from the LDR and store in word 1
  • bintoascii w1,b4,b5,b6 - convert the value in w1 into a three digit number
  • serout C.4,N9600_8,("LIGHT",b4,b5,b6, "_") - output LIGHT followed by the three digit number and then an underscore over wireless

Linux Computer

This value can be viewed on the computer, and by covering the LDR you will see the value change:

drumcoder@host:~$ stty -F /dev/ttyACM0 9600
drumcoder@host:~$ cat /dev/ttyACM0
LIGHT932_LIGHT932_LIGHT932_LIGHT932_LIGHT932_LIGHT932_LIGHT932_LIGHT933_LIGHT932_LIGHT933_LIGHT932_LIGHT933_LIGHT932_LIGHT932_
LIGHT932_LIGHT932_LIGHT784_LIGHT483_LIGHT493_LIGHT451_LIGHT541_LIGHT455_LIGHT489_LIGHT924_LIGHT933_LIGHT933_LIGHT933_LIGHT933_
LIGHT932_LIGHT933_LIGHT933_

Here the value goes from around 930 when lit to around 500 when covered.

Improvements

The bintoascii command that takes a word as the first parameter can populate five bytes - I chose to use only three here, as the first two were always 0. The program using all five bytes is:

readadc10 C.2, w1
bintoascii w1,b4,b5,b6,b7,b8
serout C.4,N9600_8,("LIGHT",b4,b5,b6,b7,b8,"_")

The output here is:

LIGHT00932_LIGHT00932_LIGHT00932_LIGHT00932_LIGHT00932_LIGHT00553_LIGHT00544_LIGHT00546_LIGHT00544_LIGHT00545_LIGHT00544_
LIGHT00628_LIGHT00931_LIGHT00932_LIGHT00932_LIGHT00932_