Haunted by Voltage Dividers

I am haunted by voltage dividers. These little buggers were the focus of my electronics lab in college for an entire semester, and I’m sorry to say that class made me hate electronics for a long time. Years later, the voltage dividers reappeared, when I had to learn about guitar modding for work–potentiometers are central to the workings of an electric guitar. And now the voltage dividers have returned, just when I thought it was safe again.

After this lab, I can say that my disdain for voltage dividers has subsided (they are substantially less scary when they aren’t accompanied by complex math). The concept behind a voltage divider is this: the output voltage of the circuit is a fraction of the input voltage. The most basic form of the circuit involves two resistors connected in series, and there are a number of variations and other circuits that are built on the structure of the voltage divider.

The first voltage divider variation I had to build for this lab involved a potentiometer. The potentiometer acts as a dimmer switch, controlling the volume of the analog signal that will be outputted to the LED.

I also printed the results to the serial monitor, so that you can see the numerical change in the output as the potentiometer knob turns.

Here is the code:

[code lang="c"]int analogVal = 0;    //stores pot or other analog value
int brightVal = 0;  //stores LED brightness value
const int LEDpin = 9;  //stores pin that LED is connected to

void setup()
{
    Serial.begin(9600);  //initialize serial at 9600 bits/s
    pinMode(LEDpin, OUTPUT);
}

void loop()
{
    analogVal = analogRead(A0);
    brightVal = analogVal/4;  //divide by 4: values too large for Arduino to process

    analogWrite(LEDpin, brightVal);  //outputs the brightness value to the LED
    Serial.println(brightVal);
}[/code]

The second circuit I had to build for this lab involved two voltage dividers, with analog input regulated by two force sensitive resistors (FSRs).

Here is the code:

[code lang="c"]int LHsensor = 0;    //storing sensor values
int RHsensor = 0;

int bright1 = 0;    //storing LED brightness values
int bright2 = 0;

const int LEDpin1 = 9;    //top (left) LED
const int LEDpin2 = 10;  //bottom (right)LED

void setup()
{
  Serial.begin(9600);  //initialize serial at 9600 bits/s
  
  pinMode(LEDpin1, OUTPUT);
  pinMode(LEDpin2, OUTPUT);
}

void loop()
{
  LHsensor = analogRead(A0);  //matching sensors to analog input pins
  RHsensor = analogRead(A1);
  
  bright1 = map(LHsensor, 0, 1023, 0, 255);  //map voltage to brightness range
  bright2 = map(RHsensor, 0, 1023, 0, 255);
  
  analogWrite(LEDpin1, bright1);  //output brightness value to the LEDs
  analogWrite(LEDpin2, bright2);
  
  Serial.print("Left Sensor = ");
  Serial.print(LHsensor);
  Serial.print("t Left Brightness = "); 
  Serial.println(bright1);
  Serial.print("Right Sensor = ");
  Serial.print(RHsensor);
  Serial.print("t Right Brightness = "); 
  Serial.println(bright2);
  delay(500);
}[/code]