Light Sensor and Motion Sensor Using ThingSpeak, Arduino, and Processing

Thingspeak.com allows one to post information gathered by a networked device in real time.  I decided that I would test out the system with a simple light sensor. Then, I went further by developing a new Processing application which detects motion using a webcam and posts it to a ThingSpeak channel.

I’ve been working with microcontrollers for some time, but Thingspeak.com is a great way to connect those microcontrollers to the world.

Light Sensor Development

First, I made a simple program to learn how to send an HTTP GET request to thingspeak.com: thingspeaktest.zip. The program sends any numeric key press as a value to the thingspeak server.

Then I made the circuit to interface the light sensor to the Arduino.

Connecting the light sensitive resistor to the Arduino.

Closeup of the finished circuit.

The code I uploaded to the Arduino couldn’t be simpler:

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.println(analogRead(0));
  delay(10000);
}

Then, I modified my original processing app so that it forwards data received from the arduino to the server: click here to download

Screen Shot of Processing App

Motion Sensor Development

After making the system with the light sensor, I had the idea to make a motion sensor using a webcam.

The Processing application essentially does the following:

  1. Takes the difference between frames continuously for thirty seconds (at only 2 frames per second to not overload the CPU).
  2. At the end of the thirty seconds, takes the max difference between frames and interprets this as the movement value.
  3. Sends this movement value to ThingSpeak.com

Screenshot of the Processing app displaying in the background the motion currently occuring in the frame.

Results

Click here for a live graph of the light sensor data. (Not active)

Click here for a live graph of the motion data. (Not Active)

My first bit of output from the system after leaving it running over night. Click for live graph of data.

After getting it to work, I checked the output after leaving it over night. I was surprised to find that in the dark, the output was a regular sawtooth wave. What is the source of this strange signal? Noize? No. I actually have a clock on the wall which displays the time as the number of dots illuminated for each digit. The sawtooth signal is due to the last digit of the time incrementing—pretty cool!

Hopefully I will be able to post some interesting motion sensor data snapshots soon.

About these ads

About Keegan

Hi, I'm Keegan.
This entry was posted in Circuits and tagged , , . Bookmark the permalink.

7 Responses to Light Sensor and Motion Sensor Using ThingSpeak, Arduino, and Processing

  1. Maddox says:

    The processing zip does not seem to match the screen output or description – do you have a more recent version that does this? I am attempting to send data to a thingspeak feed without luck…so far!

  2. Keegan says:

    Sorry for replying so late.

    There are two different files I linked to in the post: one labeledthingspeaktest.zip and the other labeled “click here to download”. Are you clicking on the second one? The first is just a test to make sure the configuration is working.

    Is there an error message? I looked through the code and noticed that the API key was hardcoded instead of using the variable at the top, so I fixed that and uploaded a new version.

    When you have more questions, I’m happy to help and I promise to reply sooner next time.

  3. ejoso says:

    Keegan – thanks a lot for this tutorial and code, it’s really helpful!

    After some playing around and following a lot of 301 errors returned from thingspeak.com, I found success posting by altering the main c.write line in the sendNumber function to the following:

    c.write(“GET /update?key=”+APIKEY+”&”+FIELD+”=”+num+ “\n”);

    Whereby I removed the HTTP/1.1 and instead just write the new line. (“\n”) I don’t get as verbose a return from thingspeak as your screengrab, however I was able to write my data reliably. I was unable to do so with the HTTP/1.1 line included.

    I guess I’d ask you – why did you include the line there? Or was it unintentional?

    Either way, I really appreciate the post as it was really helpful! Thanks!

  4. eddy says:

    Hi keegan,
    Can we fuse (fusion) together light sensor with image processing data?

    • Keegan says:

      I’m not sure what you mean by fusing light sensor and image processing data. I graphed the light sensor data as a test and then went farther by graphing the output of a simple video motion calculation the same way.

      What do you have in mind?

  5. Hello there I am so glad I found your web site, I really found you by accident,
    while I was researching on Aol for something else, Nonetheless I am
    here now and would jujst like to say cheers for a tremendous post aand a all round exciting blog (I also love
    the theme/design), I don’t have time to browse it all at the moment but
    I have book-marked it and also added in your RSS feeds, so
    when I have time I will bbe back to read more,
    Please do keep up the fantastic work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s