Quantcast
Channel: David Orlo .com | Category Archives: Arduino
Viewing all articles
Browse latest Browse all 8

Arduino – Analog Read Potentiometer to Digital Out LED + Bonus Photoresistor to LED

$
0
0

The analog to digital sketches have been covered a million ways from Sunday with every conceivable part but in order for us to move on to more complex circuits and concepts I need to be sure you know these simpler ones. This tutorial wont be quite as in depth as the others because frankly there just isn’t much code. Without further delay I give you the wiring diagram.

Arduino Analog Potentiometer to Digital LED

Another very simple circuit diagram, by the way if you have been wondering what program I am using for these diagram and its actually the most widely used program for diagramming Arduino circuits, its called Fritzing and you can get it here. Its completely Free and has tons of well known parts either built in directly or available for download on the user submission pages.

As shown above the Analog signal is positive and as the positive input increases the digital output increases as well. There is of course a pull down resistor in place which is directly connected to the potentiometer, an interested side note and the reason for the 3 legs on the potentiometer is that you can swap the positive and negative in the diagram above and the LED will get brighter when you turn the potentiometer to the left (CCW) instead of the right (CW).

/*
Analog Potentiometer to Digital LED Sketch
By: David M. Orlo
www.DaviedOrlo.com
*/
byte potPin=0; //Analog 0 connected to the potentiometer
byte LEDPin=6; //Connected to LED on Pin 6
int potValue=0; //Value returned from the potentiometer

void setup(){
  pinMode(LEDPin, OUTPUT); //Set Pin 6 as an Output
}

void loop(){
  potValue = analogRead(potPin)/4; //Read the potentiometer, convert it to 0 - 255
  analogWrite(LEDPin, potValue); //Write the converted potentiometer value to LED pin
}

So simple isn’t it? Now lets change things up a bit and make the LED pulse in relation to the potentiometer value. We can keep the same circuit layout but we need to make a few tweaks to the code.

/*
Analog Potentiometer to Digital LED Sketch
By: David M. Orlo
www.DaviedOrlo.com
*/
byte potPin=0; //Analog 0 connected to the potentiometer
byte LEDPin=6; //Connected to LED on Pin 6
int potValue=0; //Value returned from the potentiometer

void setup(){
  pinMode(LEDPin, OUTPUT); // Set Pin 6 as an Output
}

void loop(){
  potValue = analogRead(potPin); //Read the potentiometer
  digitalWrite(LEDPin, HIGH);  //Turn the LED on
  delay(potValue); //Use the potentiometer value as a length of time to pause the micro
  digitalWrite(LEDPin, LOW); //Turn the LED off
  delay(potValue); //Use the potentiometer value as a length of time to pause the micro
}

The first thing you will notice is that the LED blinks faster when you turn the pot to the left which seems opposite to whats intuitive, I will let you figure out how to reverse this either in code or in hardware using the hint I gave you above about the potentiometer.

Now for the Photoresistor, the circuit is very similar as you will see below.

Arduino PhotoResistor to LED Blink

The code is exactly the same so no changes needed there, You will notice I used a 1K resistor instead of a 10K thats used in other examples. In my example above you will notice the LED blinks faster as the photoresistor gets darker, lets say you want to reverse that and make it blink slower, simple just reverse the GND and POS and swap out the 1K resistor for a 10K.


Viewing all articles
Browse latest Browse all 8

Trending Articles