Skip to content

Chapter 7: Working with Analog I/O

Digital I/O is about binary states: HIGH or LOW, 0 or 1. But the world around us is often not binary; it’s analog. From the gentle rise and fall of sunlight to the varying tones of music, analog signals permeate our lives. Arduino allows us to interact with this analog world, making our projects more dynamic and responsive.

Understanding Analog Signals

An analog signal is a continuous signal that represents physical measurements. In contrast to digital signals, which have discrete values, analog signals can have an infinite number of values within a range.

Arduino boards can't process infinite resolutions, so they use Analog-to-Digital Converters (ADC) to translate continuous analog signals into a set of discrete digital values. For many Arduino boards, this resolution is 10 bits, meaning they map analog signals to one of 1024 digital values (ranging from 0 to 1023).

Reading Potentiometers and Light Sensors

1. Potentiometers:
A potentiometer, often known as a "pot", is a variable resistor. Turning its knob changes the resistance, which can be read as a voltage change by the Arduino.

int potPin = A0;      // Connect potentiometer to analog pin A0
int potValue = 0;     // Variable to store the potentiometer's value

void setup() {
  Serial.begin(9600);  // Start serial communication
}

void loop() {
  potValue = analogRead(potPin);  // Read the pot's value
  Serial.println(potValue);       // Print the value to the serial monitor
  delay(100);                     // Short delay for stability
}

2. Light Sensors (Photoresistors):
A photoresistor, or Light Dependent Resistor (LDR), changes its resistance based on the amount of light it receives. The darker it gets, the higher the resistance.

int ldrPin = A1;       // Connect LDR to analog pin A1
int ldrValue = 0;      // Variable to store the LDR's value

void setup() {
  Serial.begin(9600);   // Start serial communication
}

void loop() {
  ldrValue = analogRead(ldrPin);  // Read the LDR's value
  Serial.println(ldrValue);       // Print the value to the serial monitor
  delay(100);                     // Short delay for stability
}

Analog Output Using PWM

While Arduino boards cannot produce true analog outputs, they can simulate them using Pulse Width Modulation (PWM). PWM works by varying the width of the "on" pulse in a pulse train. For devices like LEDs, our eyes average out the flashing to perceive dimming.

int ledPin = 9;          // LED connected to digital pin 9 (a PWM pin)
int brightness = 0;      // LED brightness

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // Gradually increase the LED's brightness
  for (brightness = 0; brightness <= 255; brightness++) {
    analogWrite(ledPin, brightness);   // Write PWM value to LED
    delay(10);
  }

  // Gradually decrease the LED's brightness
  for (brightness = 255; brightness >= 0; brightness--) {
    analogWrite(ledPin, brightness);   // Write PWM value to LED
    delay(10);
  }
}

Conclusion

Through the interplay of analog and digital, Arduino bridges the gap between the binary world of computers and the continuous nature of our environment. By understanding analog I/O, you unlock the potential to interact with a myriad of sensors, devices, and stimuli, further enriching your projects and innovations.

Previous article Chapter 8: Serial Communication
Next article Chapter 6: Working with Digital I/O

Leave a comment

Comments must be approved before appearing

* Required fields