Analog Inputs

Materials Needed:

  • (8) 5mm red LED
  • (8) 270 Ω Resistors (red, purple, brown stripes)
  • (1) 74HC595 Shift Register
  • (1) 10 kΩ variable resistor (pot) short for "potentiometer"
  • (1) Half-size breadboard
  • (1) Arduino Uno R3
  • Jumper wires

Code:

int potPin = 0;

int latchPin = 5;

int clockPin = 6;

int dataPin = 4;

 

int leds = 0;

 

void setup() 

{

  pinMode(latchPin, OUTPUT);

  pinMode(dataPin, OUTPUT);  

  pinMode(clockPin, OUTPUT);

}

 

void loop() 

{

  int reading  = analogRead(potPin);

  int numLEDSLit = reading / 114;  //1023 / 9

  leds = 0;

  for (int i = 0; i < numLEDSLit; i++)

  {

    bitSet(leds, i); 

  }

  updateShiftRegister();

}

 

void updateShiftRegister()

{

   digitalWrite(latchPin, LOW);

   shiftOut(dataPin, clockPin, LSBFIRST, leds);

   digitalWrite(latchPin, HIGH);

}

Layout:

Schematic: