Sunday, May 16, 2010

More Arduino Reading Optical

Here's a code snippet for you, all dirty and messy. It will be cleaned up prior to inclusion in the class. Currently working on including the TSL230R Light-to-Frequency Converter with code shamelessly borrowed from:
http://roamingdrone.wordpress.com/2008/11/13/arduino-and-the-taos-tsl230r-light-sensor-getting-started/

I will probably use the code as-is. It handles this device as an interrupt process, and handling interrupts was an intention of the class.

I have this mystery phototransistor from Tanners. No datasheed can be found by several people working on int and Tanners sells the item "No data". It looks like a TO-18 case with a glass top, so I will assume that the connections are the same. It has a base pin, it would seem, which I thought was odd as I thought that that base was controlled by light. I have several, so I will experiment with a few, but I'm not sure how I will know if I have destroyed one or not. I think I may skip this one for another day and leave it out of the class.

----- Code 4 You  -------

/*
IR1
*

detects moving infrared with PIR. Upon sensing, checks light level with Light Dependent Resistor (Cadmium photocell) and then uses infrared rangefinding to determine range of object.

*/


int IRrangePin = 0; // select the input pin for the potentiometer
int inPin = 1; // select pin for IR detector (analog)
int LRpin = 2; //select analog pin for LDR
int ledPin1 = 13; // select the pin for the LED
int ledPin2 = 12; // second led
int ledPin3 = 11; // third led
int val = 0; // variable to store the value coming from the sensor
int dval = 0; // variable to store valud from IR detect
int Lval = 0; // Variable for use with LDR
int NewVal = 0; // Work variable



void setup() {
Serial.begin(9600);
Serial.println("Setup. ");
pinMode(ledPin1, OUTPUT); // declare the ledPin1 as an OUTPUT
pinMode(ledPin2, OUTPUT); // declare the ledPin2 as an OUTPUT
pinMode(ledPin3, OUTPUT); // declare the ledPin3 as an OUTPUT
pinMode(inPin, INPUT); // declare the Infrared detect pin as an INPUT
pinMode(LRpin, INPUT); // declare teh LDR pin as an INPUT
digitalWrite(ledPin3, LOW); // full on
}

void loop() {
dval=LOW;
detect();
val = analogRead(IRrangePin); // read the value from the sensor

/*
Serial.print("Proximity: "); Serial.println(val);
digitalWrite(ledPin1, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin1, LOW); // turn the ledPin off
*/
delay(val); // stop the program for some time
}


void detect()
{
delay(100);
dval = analogRead(inPin); // read input from IR detector
//Serial.print(dval);
if (dval>0)
{
Serial.println();
Serial.print(" Detection >YES. ");
digitalWrite(ledPin2, HIGH); // turn the ledPin on
LDR();
findRange();
}

if (dval<=0)
{
Serial.print(".");
digitalWrite(ledPin2, LOW); // turn the ledPin off
}
delay(200);
}


void LDR(){
Lval = analogRead(LRpin);
Serial.print(" LDR: "); Serial.print(Lval); Serial.print(" ");
analogWrite(ledPin3,(75+Lval));
delay(100);
}


void findRange(){
val = analogRead(IRrangePin); // read the value from the sensor
NewVal = val/100;
Serial.print("Proximity: "); Serial.print(val);

switch (NewVal) {

case 0:
//far away
Serial.print(" Too far. ");
break;
// break is optional

case 1:
//do something when var == 1
Serial.print(" Zone 1. ");
break;

case 2:
//do something when var == 2
Serial.print(" Zone 2. ");
break;

case 3:
//do something when var == 3
Serial.print(" Zone 3. ");
break;

case 4:
//do something when var == 4
Serial.print(" Zone 4. ");
break;

case 5:
//do something when var == 5
Serial.print(" Zone 5. ");
break;

case 6:
//do something when var == 6
Serial.print(" Zone 6. ");
break;

case 7:
//do something when var == 7
Serial.print(" Zone 7. ");
break;

default:
// if nothing else matches, do the default
// default is optional
Serial.print(" Unknown. ");
}

Serial.println();
digitalWrite(ledPin1, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin1, LOW); // turn the ledPin off
delay(val); // stop the program for some time

}

Tuesday, May 11, 2010

Arduino Reading Optical

Here is what is currently in production for the Arduino project. It is a draft and not finished work.

Reading Optical

Many optical devices are analog and not digital. The task is often to convert the response of the analog device into a more-or-less digital response. In the application of the analog devices in this segment, the analog pins on the Arduino chip will be used. Pins 0, 1 and 2 will be used. The analog pins on the Arduino read the input voltage from the device and convert into a numeric value.

The Arduino reference manual states, “This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit.”

Note that the voltages are still between 0 and 5 volts. Be careful not to fry your Arduino.

The numeric values from the analog pins may then be tested and branched using ranges of values and a case statement; an “if” structure will also work. Synthesizing digital action from an analog device merely means that the voltage has crossed a certain threshold; the number returned from the pin has passed a certain value and a test becomes true of false accordingly.

Goal

Instruct the student on input sensors- light (including optical encoder), switches and potentiometer. Optical encoders and switches are the “more binary” devices in the group; other items are “more analog”

Prerequisites

No prerequisites beyond the basic Breadboard Arduino class. These programs cover new material for this class and do not build on previous material.

Method

Cover analog versus digital input. Reiterate voltage capacities of the Arduino (0-5 volts). More information is available through the serial output than with LEDs in this segment.

Notes

There are multiple circuits on this board. Neither takes up much breadboard space or pins on the Arduino. If the infrared rangefinder is the Sharp brand, then a connector will most likely need to be built. In this case, a three pin, 0.10 inch pitch header pin was soldered to the end of the special pigtail for the sensing unit. After soldering, insulation and stability were needed, so a blob of hot-melt glue was added to the header pin.

The Parallax PIR unit, without a pigtail will only fit on the corner of the board due to I/O pin placement and jumper pin placement. A couple of ling wires are needed to connect to the Arduino. Other components are fairly straightforward.

The programs have some serial output to be displayed on the computer monitor. Power indicators may be left off.

The Class

Optical sensors are sensitive to light. Infrared (IR) has been grouped into this segment as it is looked at as both heat and light. It is light that cannot be seen with the naked eye, but certain electronic devices are sensitive to it. The remote control for your television is an IR device. For some reason, cameras on cell phones are sensitive to IR light. If you aim your TV remote at your cell phone camera, you will be able to see the light produced by the remote. There is more going on than just an IR flashlight, but at least you can verify that the batteries are not dead.

In this class, there are three devices- a simple photo resistor that passes power as a function of the amount of light striking it. This particular device is translucent and as such, is also sensitive to some of the light striking the back of the device. As more light strikes the surface, the resistance lowers. When the resistance lowers, more voltage appears on the Arduino analog pin and the Arduino returns a higher numerical value.

The infrared rangefinder uses reflected infrared light to determine range up to about 50 inches. Since it is using light, it is also susceptible to interference- things that would also get in the way of visible light. It is also sensitive to the color of the reflective surface. If sensing people, different colored clothing will yield different responses. It is not an exact science.

The Passive Infrared (PIR) device works the same way as the motion detector lights that light your driveway when someone walks by. It has a series of Fresnel lenses over an IR-sensitive device. When the device senses IR moving from one lens to another, a signal is produced. This device is also sensitive to distance. This particular PIR is a Parallax device designed for hobbyists.


Monday, May 10, 2010

Package from Sparkfun redux

It came- hooray for Sparkfun! Well, it probably came on Friday, but I wasn't there to receive it. I had it shipped to work thinking for some reason that it would get int my hands faster. In reality, the opposite was true. I received five twenty-eight pin chips with the little red dot. Now, I have to find out how to return the non-arduino chips. I'm pretty sure they have a procedure for that.

So, the big lesson here is that Sparfun made a boo-boo. Hey- it happens. The important thing is that they corrected the situation in a timely manner. When you next need electronic components- how about checking http://www.sparkfun.com/ first?

I should get back to work.

Monday, May 3, 2010

Arduino Light Dependent Resistor

Well, tonight, the board has a light dependent resistor (LDR). Only two of the three leads are used, so it's a little tacky, but this is, after all, prototyping. It will be cleaned up later- functionality is the name of the game right now. The analog value returned from the LDR drops with the light level. Using this value will enable the implementation of a small night-light circuit rather easily.

The easiest approach is to take the value returned from the LDR and feed it into an analogWrite command and use PWM to alter the brightness of the LED. Doing this directly would normally dim the LED as the light was dimmed. The LED was reversed and now operates as the inverse of the value read from the LDR. That is, as the light dims, the LDR returns a lower value, that value is fed into the analogWrite command and as the value drops, the LED gets brighter. Quick, easy, effective.

Here is the code in action:



Here is the board in action:


Happy breadboarding!

Sunday, May 2, 2010

Arduino Infrared

OK-
Nothing from Sparkfun yet, but weekend delivery was not expected- They need time to respond. So the next best thing was to borrow an Arduino off another board and get moving again.

The current project is an optical board with Arduino. Starting with infrared which may not actually be classified as optical, but it will be for this board. Passive Infrared (PIR) coupled with an infrared rangefinder makes sort of a neat human detector (or dog, cat, rats, what-have-you). Currently, the two items operate independently, but the code will be changed to let the PIR drive. If a heat source is sensed, then the rangefinder will be engaged. Perhaps. There are other approaches.

For your viewing pleasure, here is a picture of the board so far. It's a little blurry, but you should be able to get the point.


The PIR is at the bottom right, the IR rangefinder is at the upper right with the cable pinned under the breadboard. The LEDs indicate the power is on for the PIR and it has sensed motion, and power is on for the IR rangefinder and it has not yet been engaged.

More news as it happens.