Let's use the analog to digital converter

  • Analog input is also a very important element
  • In this example potentiometer will be used as an example to use analog input

Pre-requisite:

  1. Finished Starting to play with Arduino
  2. Optional Dimming LED
  3. Basic circuit knowledge

Objectives:

  1. Use analogRead to read AD converter on Arduino.

Descriptions:

  1. Please refer to : http://www.arduino.cc/en/Tutorial/Potentiometer 

     

  2. The original programme will blink the led using the input as speed of blinking, It's also possible to adjust the LED intensity using the input.

    int potPin = 0;    // select the input pin for the potentiometer
    int ledPin = 9;   // select the pin for the LED (need to be any pin you connected and need to have a "~")
    int val = 0;       // variable to store the value coming from the sensor
    
    void setup() {
      pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
    }
    
    void loop() {
      val = analogRead(potPin);    // read the value from the sensor
      analogWrite(ledPin, val >> 2); // because the read value is 0 -> 1023, the output range is 0 -> 255, 
                                     // >> 2 basically is a faster way of dividing by 4
    }
  • No labels