2010年6月7日月曜日

Exercise and Tutorial 3

http://www.lulu.com/items/volume_63/1108000/1108699/4/print/ARDUINO_NOTEBOOKv6.pdf

This exercise based on the PDF p32,p34 "pwm output" out put and I modified a program to check my understanding.

The term pwm output is Pulse-width modulation that allow to use digital output like as analog output.

The following circuit and program will adjust the brightness of the  LED by the darkness of the place with photo-sensor and pwm.

Requirements

220Ω resistor

more than 10kΩ resistor (depend on the darkness of a room. I use 21kΩ)

1kΩ resistor(not necessary to use 1kΩ)

LED

Photo-sensor

Wires

Arduino 

Program

/*

Basically, this program works like a bridge between analog input and pwm output.

Get a value from photosensor and do calculation to adjust a value for pwm with arithmetics. 

*/

int ledPin = 9; // PWM pin for the LED
int anaPin=0;
int val;


void setup(){
Serial.begin(9600);
Serial.println("start");
}
void loop()
{
 val = analogRead(anaPin)/4; 

/*

The program get a vale from analog input.

However, the value of analog input takes from 0 to approx. 1000

,but for the pwm we need a value from 0 to 255.

Therefore, the value is divided by 4.

*/
Serial.println(val); // just for checking a value 
val = 255 - val;

/*

Since we want to light the LED when a room became dark,

we need to do kind of flip the value. 

So that we need to subtract a value from 255.

*/
analogWrite(ledPin, val);// send a signal and value to the pin 
delay(100);
}



Hardware setup



Need to adjust the resistance of resisters case by case.

Work movie





0 件のコメント:

コメントを投稿