arduino:arduino_crash_course:analog_input
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
arduino:arduino_crash_course:analog_input [2012/11/06 01:12] – [Night light] mithat | arduino:arduino_crash_course:analog_input [2017/12/06 01:27] (current) – [Night light] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Analog Input ====== | ====== Analog Input ====== | ||
- | As we have seen, an Arduino digital input is one that have one of two states: HIGH or LOW. Most Arduino implementations also permit you to connect an input whose value can be any value between 0 volts and (by default) the power-supply voltage and then convert the input level to an internal digital | + | As we have seen, an Arduino digital input is one that have one of two states: HIGH or LOW. Most Arduino implementations also permit you to connect an input whose value can be any value between 0 volts and (by default) the power-supply voltage, and then convert the input level to an integer |
- | Consult the documentation for your specific Arduino board to detmine | + | Consult the documentation for your specific Arduino board to determine |
===== Night light ===== | ===== Night light ===== | ||
Line 10: | Line 10: | ||
<WRAP center round tip 60%> | <WRAP center round tip 60%> | ||
- | CdS cells are notoriously tweaky devices. A lot of experimentation with your particular cell will probably be required to see what your circuit is actually doing--quite likely requiring the use of the serial communication to report the ADC values. | + | CdS cells are notoriously tweaky devices. A lot of experimentation with your particular cell will probably be required to see what your circuit is actually doing---quite likely requiring the use of the serial communication to report the ADC values |
- | There are more reliable and calibrated light-level sensors available that may be a better (but significanly | + | There are more reliable and calibrated light-level sensors available that may be a better (but significantly |
</ | </ | ||
+ | FIXME We need a circuit diagram | ||
- | TODO: | + | In the following example, we output the light-level reading to serial communication. This should help you get a feel for the range of numbers that result from for your particular unit. |
- | | + | |
- | - hook up CdS and find a resistor that gets decent range | + | <file c cds2serial.ino> |
- | | + | /* |
+ | CdS to Serial | ||
+ | | ||
+ | */ | ||
+ | |||
+ | const int inputPin = A0; // use analog | ||
+ | const int ledPin = 13; // connect the LED to pin 13 | ||
+ | |||
+ | int lightLevel = 0; // used to store the input value | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | lightLevel = analogRead(inputPin); | ||
+ | Serial.println(lightLevel); | ||
+ | delay(500); | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | And here is the working nightlight. You may have to adjust '' | ||
+ | |||
+ | <file c NightLight.ino> | ||
+ | /* | ||
+ | | ||
+ | Turn an LED on when the ambient light is below a certain value. | ||
+ | */ | ||
+ | |||
+ | const int lightThreshold = 800; // value below which LED turns on | ||
+ | const int inputPin = A0; // use analog 0 (A0) as input pin | ||
+ | const int ledPin = 13; // connect the LED to pin 13 | ||
+ | |||
+ | int lightLevel = 0; // used to store the input value | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | lightLevel = analogRead(inputPin); | ||
+ | |||
+ | if (lightLevel <= lightThreshold) | ||
+ | digitalWrite(ledPin, | ||
+ | else | ||
+ | digitalWrite(ledPin, | ||
+ | } | ||
+ | </ |
arduino/arduino_crash_course/analog_input.1352164362.txt.gz · Last modified: 2012/11/06 01:12 by mithat