| Subcribe via RSS

Wireless Light Sensor (Part 1 of 3)

October 29th, 2009 Posted in AIR, Arduino, Electronics, Flash, Flex

In my previous article I introduced the Arduino and the XBee. The Arduino is a electronics prototyping platform, while the XBee provides wireless data transfer via 802.15 (ZigBee). Prior to that I talked about the physics behind a photocell, and showed you how one works with the Phidgets environment. Now we will take that knowledge about photocells and combine it with the Arduino and XBee to create a wireless remote light sensor that reports into a Flash application (using Flex for charting).

We will start by getting our circuit to run on the Arduino, without any of the additional complexity of a user interface or wireless data transfer. After that we’ll get a Flash user interface to work on top of the wired version of our circuit. Finally we’ll configure and introduce wireless into the mix. It’s a lot to cover, and it uses a variety of technologies and languages, but stick to it, and you’ll find a whole new world of human-computer interaction.

The Arduino

In starting with the Arduino, first connect the board to your computer via USB. Once it is connected, open the Arduino IDE. With the IDE open, select the port that represents your Arduino (Tools -> Serial Port). This can take a little trial and error to get right, but on my MacBook Pro, the Arduino shows up as “/dev/cu.usbserial-A9005aT8”.

If you’re having trouble figuring out which menu item is the Arduino, you might select a sample program, called a “sketch” in the Arduino vernacular, and try to upload it to the board. A good test is File -> Examples -> Digital -> Blink, which will blink the LED on the Arduino board. If the IDE cannot make a connection to the Arduino to upload the sketch, then it will spit out an error. If you see that error, select a different port and try again.

With the Arduino selected and connected, it’s time to get busy writing some code. A sketch begins in a function call “setup().” That’s a good place to initialize variables. After that it will call a function named “loop()” over and over, as fast as it can, for as long as the program is running. This is where the bulk of the action occurs, and where you’ll check Arduino sensor values.

const int photoCellPin = 0;
 
void setup() 
{
  Serial.begin( 9600 );
}
 
void loop()
{
  int photoCell = analogRead( photoCellPin );
 
  Serial.println( photoCell, DEC );
  Serial.print( 0, BYTE );  
 
  delay( 100 );
}

In the case of the photocell, we will want to communicate the value over serial to be able to get it into our computer, and eventually into Flash. This is done using the “Serial” class that is part of the Arduino library. There’s a method on the class called “begin()” which will open the serial connection at a given baud.

In the loop structure, we’ll check for the photocell value. Keep in mind that a photocell is just a resistor, so it is an analog connection. I connected my photocell to analog port zero (0) on the Arduino. Each loop iteration will then call the “analogRead()” function that comes with the Arduino. The result is an integer that will show the value of the current coming across the photocell.

There is a pretty deep body of information I’m glossing over here as it pertains to reading the analog value. At a high level, we’ll be passing five volts (5V) through the photocell. The Arduino has an analog-to-digital converter that maps voltage from 0V to 5V as 0 to 1023 integer values. The voltage reading across our circuit (wired in series) will require a resistor to show any value other than 1023. The value you get may differ on the resistance of your photocell and of the additional resistor value used.

Next, we want to get this out of the Arduino and into the computer, so we’ll use an Arduino method on the Serial class call “println()”. After that you’ll note that the Arduino will then send a zero-byte. This is only required because it queues XMLSocket in Flash to raise an event that it has received a chunk of data. Without this extra zero-byte, XMLSocket would continue to think it was still getting a single block of data. In turn, the event that data has arrived in Flash would never get thrown, and we would never update the user interface.

There is also one last line that calls the “delay()” function provided by the Arduino. Left to it’s own devices, the Arduino might potentially sample the analog port up to 10,000 times per second. That’s just too fast for the serial transfer to keep up. Consider also that the average Flash movie updates around 24 times per second. Even if we could get data that fast, we couldn’t effectively render it, and the person viewing it probably wouldn’t make any sense of it anyways. To resolve this problem we tell the Arduino to wait 100 milliseconds before running the loop again.

Photocell Wiring

With our code in place, we can click the upload button on the Arduino IDE and watch our program run. The photocell has a resistance of approximately 1k in light and 10k in dark, and the resistor is another 2.2k. The key word there being “approximate.” I found my photocell actually had a broader range, which resulted in values between approximately 180 and 900. How do you see that? The Arduino IDE has a built-in serial monitor – just click the button to bring up the window, and then watch the values arrive.

photocell-serial

In the next part in this series we will get this value into Flash. For that I will refer you to an older post that outlines the basic flow of data from hardware into Flash. It’s important to understand these steps as all of these experiments build on one another to get us to our final destination of a wireless light sensor.

2 Responses to “Wireless Light Sensor (Part 1 of 3)”

  1. Richard Orelup Says:

    Thanks for putting out all these posts on this subject. I’ve recently been looking at using the Aurduino myself for some home automation and monitoring stuff (a BBQ smoker that can control the temp itself and give stats over the web) and this info is really helping a lot. I’m a Web Dev by trade and it’s great to see these devices looked at from this angle since it gives me a heads up where to start. Look forward to your future posts on this subject.

    Thanks


  2. Mariano Rouillier Says:

    Just thought you’d be interested to know that I have added you to my bookmarks You make valid points in a concise and pertinent fashion, This is a really good read for me, thank you for your time. one of my articles hope u enjoy reading free online paid surveys


Leave a Reply