Potentiometer

Hi guys, i was wondering how it would be possible to add a Potentiometer to my UNO Arduino board to control the speed of the LED's blinking. I have included a picture below of what my board currently looks like. Its working fine in terms of when the button is pushed down the LED's light up in a sequence. But now i would like to add the Potentiometer to improve my board, so any help would be greatly appreciated.

Read & understand this: Potentiometer with Delay and then move on to: LED chaser

Ray

Not sure what i’m doing wrong here, sorry if its really obvious. I’m new to all of this.

I can't see anything plugged into the analogue inputs.

Can you just draw what you've got?

Sorry that was probably a pretty bad image to use, does this make it clearer?

Arsenal26:
Sorry that was probably a pretty bad image to use, does this make it clearer?

NO.

Post a drawing of how everything is connected. A photo of a pencil drawing will be fine.

…R

The center lead of the potentiometer (sometimes referred to as the wiper) shown going to digital output 13 needs to be connected to an analog input. Any one of the pins marked A0-A5 will do. You also need to connect the AREF pin to the +5 volt supply.

Like this?

its still not working for me.

This is the code i am using.

int potIn = 13; // Potentiometer analog input at analog pin 0
int ledPin = 11; // LED at digital Pin 11
int potVal = 0; // Initializing Potentiometer’s input as zero

void setup() {
pinMode(11,OUTPUT); // Setting pin 12 as OUTPUT

}

void loop() {
potVal = analogRead(potIn); // analog value read at analog pin 0 assigning to potVal
for (int brightness = potVal; brightness <= 200; brightness+=5) { // for loop for adjusting brightness
analogWrite(ledPin, brightness); // value of brightness is analog write to pin 11

}

}

So, what is it doing?

Pin 13 is not an analogue input.

You added a wire, all that was needed was to move one end of the existing wire from pin 13 to A5. So, now you need to remove the wire from Uno pin 13 to the center lead of the pot.

I’m not sure what you’re doing with the for loop and since it does not work, why don’t you start with the AnalogInput example in the IDE? I’ve changed the code below to reflect your I/O configuration. Copy it and paste it into a new sketch and give it a try.

/*
  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13.
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead().

 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground

 * Note: because most Arduinos have a built-in LED attached
 to pin 13 on the board, the LED is optional.


 Created by David Cuartielles
 modified 30 Aug 2011
 By Tom Igoe

 This example code is in the public domain.

 http://arduino.cc/en/Tutorial/AnalogInput

 */

int sensorPin = A5;    // select the input pin for the potentiometer
int ledPin = 11;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);
}

Hi, try this, I have edited your code to show where you can simplify and where some errors were made.

// int potIn = 13;                                        // Potentiometer analog input at analog pin 0
// your potIn is NOT 13 it is A0
int potIn = A0;
int ledPin = 11;                                    // LED at digital Pin 11
int potVal = 0;                                     // Initializing Potentiometer's input as zero
int brightness=0;
void setup() {
     pinMode(11,OUTPUT);                  // Setting pin 12 as OUTPUT
     // Sorry you are setting PIN 11 as output, please edit the comments as you adapt sketches.
}

void loop() {
     potVal = analogRead(potIn);         // analog value read at analog pin 0 assigning to potVal
     //for (int brightness = potVal; brightness <= 200; brightness+=5)             
     // Your potVal will be between 0 and 1023, your PWM values can only be 0 to 255
     // Use a function called MAP, look it up in the references.
     brightness = map(potVal,0,1023,0,255);
     analogWrite(ledPin, brightness);    // value of brightness is analog write to pin 11
     // You do not need a loop, the void loop() does this for you
}

I have compiled it, but not loaded it to an Arduino, so see what happens.
The pot will control the brightness of the LED on pin 11, if LED dos not light, check that you have it connected the right way.
Hope it works.
Tom… :slight_smile: