Arduino Tutorial Series : Lesson 06 - Analog Input

In the previous tutorial we looked in to Analog outputs from Arduino, What about analog inputs then ??? Can we read analog voltage values into Arduino?

Yes we can !!! Lets get started with an example...


LED Dimmer

Hardware

What you need,

  • an LED
  • 220 ohm resistor
  • 10k ohm  potentiometer



Connect the LED to PWM pin 6 and connect the adjust pin of potentiometer to analog in pin 0. Connect other two pins of pot to 5v and GND.

Code

Download Arduino code analogInput.ino



















In the code in every loop we read the analog voltage at Analog pin A0.

sensorValue = analogRead(kPinPot);

Note:

In Arduino analogRead the voltage values are read as 10 bit digital value. It means 0 to 5v are read as integer value 0 to 1023.

We need to map the value got from analogRead to a value we can output as PWM signal.
So, we need to map the value 0 - 1023 to 0 - 255.

ledBrightness = map(sensorValue, 0, 1023, 0, 255);

Then we can use analogWrite to output to voltage to LED pin.

Thats it, Upload the sketch to arduino and turn the knob of potentiometer. The brightness of LED will change according to the pot position.

This technique can be used for many applications, tryout different things with your Arduino. You will learn a lot by as you use it for your projects.

No comments:

Powered by Blogger.