Computing stuff tied to the physical world

DIJN.11 – Connect the light sensor

In Uncategorized on Feb 21, 2013 at 00:01

Welcome to the eleventh instalment of Dive Into JeeNodes. Let there be light!

Everything until now was nice, but wasn’t really about sensing the environment. For that, we need to hook up some sensors, obviously. One of the simplest sensors around is the Light Dependent Resistor, or LDR, as it’s usually called. It does exactly what its name says: vary its own internal resistance, depending on the amount of light falling on it.

To read it out with an ATmega, all we need to do is connect the LDR between ground and an analog I/O pin, and enable the ATmega’s internal pull-up resistor for that I/O pin to get a voltage drop over the LDR. Let’s first hook up the LDR:

DSC_4370

As you can see, I’ve connected the LDR to the analog pin of Port 1 on the JeeNode (a JeeNode USB in this case – but any type of JeeNode will do).

The next step is to upload the proper code to the JeeNode, so that it actually does the measurement and transmits the result over wireless. This is a task for the Arduino IDE. We’ve already used it to load the initial test1 sketch, and now we can replace it with test2:

#include <JeeLib.h>

#define LDR 0 // the LDR will be connected to AIO1, i.e. analog 0

void setup () {
  // this is node 1 in net group 100 on the 868 MHz band
  rf12_initialize(1, RF12_868MHZ, 100);

  // need to enable the pull-up to get a voltage drop over the LDR
  pinMode(14+LDR, INPUT_PULLUP);
}

void loop () {
  // measure analog value and convert the 0..1023 result to 255..0
  byte value = 255 - analogRead(LDR) / 4;

  // actual packet send: broadcast to all, current counter, 1 byte long
  rf12_sendNow(0, &value, 1);

  // let one second pass before sending out another packet
  delay(1000);
}

(rf12_sendNow was recently added to JeeLib, make sure you’re using a recent version)

If you have followed along to the letter of the series, then the JeeNode will still be hooked up, and the IDE will still have the proper serial port setting for it. If not, please go back and make sure you can upload a new sketch. It’s a matter of choosing the proper serial port, selecting the examples/DIJN/test2 sketch in the IDE, and clicking on the Upload button. If all went well, you’ll see this:

Screen Shot 2013-02-16 at 11.40.21

That’s it. If you now look at the Demo page in HouseMon again via your browser, you’ll see actual light readings. Put your hand over the LDR and see the value decrease in real-time (within a second, in this case, since that’s how often the JeeNode sends out a new packet).

As you’ll see, the LDR is very sensitive. It has to be pretty dark for the value to drop under 100 (it ranges from 0..255). The range is also not linear or calibrated in any way.

Congratulations… now you have a real WSN! Tomorrow we’ll take care of some loose ends.

Onwards!

(This series of posts is also available from the Dive Into JeeNodes page on the Café wiki.)

  1. Got it working with a bit of a fiddle, I may have got myself confused but should both the sender and the receiver have the same node ID (1) I changed the ID on the sender to 2 and the packet.buffer[0] to [1] (in demo.coffee) and then it started working, until I did this it wouldn’t even show in the Putty window. As I said this could be a result of me messing around though.

    • It sounds like you are working with an older version of the HouseMon code. Please consider reverting that change and doing a “cd ~/housemon && git pull”. Things have been changing a lot – I’m currently working on solidifying the most important pieces. Same node ID would be ok for now, but please revisit the setup of the JeeLink (or whatever node type you use) – node ID 31 is best for a central node, due to future “announcer” packet functionality.

  2. Did as you suggested and the demo page now updates with the new value OK but there is no buffer data showing in the Putty window as before, strange, but it is working.

    One thing I noticed when logging onto the server with my Android phone, the menu on the right (Home, Admin, Demo) doesn’t appear, just an arrow that doesn’t do anything.

  3. OK will do.

    Thanks, that stacks up with what I’ve seen so far.

  4. There is a little bug in the 1st sentence of this post.

  5. I am having problems with test2

    rf12_initialize(1, RF12_868MHZ, 100); and rf12_sendNow(0, &value, 1);

    error messages “was not declared in this scope”

    I have uploaded latest JeeLib from Github and tried re-installing Arduino 1.03,with no success, sorry but need help

Comments are closed.