User Tools

Site Tools


arduino:arduino_crash_course:analog_input

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
arduino:arduino_crash_course:analog_input [2012/11/06 01:04] mithatarduino:arduino_crash_course:analog_input [2017/12/06 01:27] (current) – [Night light] mithat
Line 1: Line 1:
 ====== Analog Input ====== ====== Analog Input ======
  
-An Arduino digital input is one that have one of two states: HIGH or LOW. However, most Arduino implementations also permit you to connect an input whose value can be any value between 0 volts and the FIXME power-supply voltage and then convert the input level to an internal digital value between 0 an 1204. The Arduino literature refers to these as "analog" input. The process of coverting true analog signal to a digital number is called **analog to digital conversion** (**ADC**).+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 voltageand then convert the input level to an integer value between 0 an 1023. The Arduino literature refers to these as "analog" inputs. The process of converting physical analog signal to a digital representation is called **analog to digital conversion** (**ADC**).
  
-Consult the documentation for your specific Arduino board to detmine which pins support analog input.+Consult the documentation for your specific Arduino board to determine which pins support analog input. On the Arduino UNO, inputs A0 through A5 can be used as analog inputs.
  
-===== Night Light =====+===== Night light =====
  
 The following example uses an analog input and a digital output to build a simple night-light. A cadmium-sulfide photo-sensitive resistor (commonly called CdS cells) is used in a voltage divider setup to produce a voltage that changes with the amount of light incident on the sensor. If the voltage is above a certain threshold, the Arduino will turn on the LED. The following example uses an analog input and a digital output to build a simple night-light. A cadmium-sulfide photo-sensitive resistor (commonly called CdS cells) is used in a voltage divider setup to produce a voltage that changes with the amount of light incident on the sensor. If the voltage is above a certain threshold, the Arduino will turn on the LED.
  
 <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 (see [[light_meter|this]] for an example).
  
-There are more reliable and calibrated light-level sensors available that may be a better (but significanly more expensive!) choice if you wish not to deal with the tweakiness of Cds cells.+There are more reliable and calibrated light-level sensors available that may be a better (but significantly more expensive!) choice if you wish not to deal with the tweakiness of Cds cells.
 </WRAP> </WRAP>
  
 +FIXME We need a circuit diagram
  
 +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.
  
 +<file c cds2serial.ino>
 +/*
 + CdS to Serial
 + Ouptut the reading on the CdS pin to Serial.
 + */
  
 +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() {
 +  Serial.begin(9600);            // initialize serial communication
 +  pinMode(ledPin, OUTPUT);       // make LED's pin an output
 +}
 +
 +void loop() {
 +  lightLevel = analogRead(inputPin);  // get an analog reading      
 +  Serial.println(lightLevel);         // print out the reading
 +  delay(500);                         // delay between reads
 +}
 +</file>
 +
 +And here is the working nightlight. You may have to adjust ''lightThreshold'' so it works well with your particular CdS cell.
 +
 +<file c NightLight.ino>
 +/*
 + NightLight
 + 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, OUTPUT);       // make LED's pin an output
 +}
 +
 +void loop() {
 +  lightLevel = analogRead(inputPin);  // get an analog reading      
 +
 +  if (lightLevel <= lightThreshold)
 +    digitalWrite(ledPin, HIGH);       // turn the LED on
 +  else
 +    digitalWrite(ledPin, LOW);        // turn the LED off
 +}
 +</file>
arduino/arduino_crash_course/analog_input.1352163850.txt.gz · Last modified: 2012/11/06 01:04 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki