Explore the world of sensors and digital/analog inputs
🔍 From hardware to sensing the world around us
Sensors are devices that detect and measure physical properties from the environment and convert them into electrical signals that Arduino can understand. They are the "eyes and ears" of your Arduino projects.
Buttons and switches are the simplest digital input devices. They provide a direct way for users to interact with your Arduino projects.
// Basic Button Reading Example
const int buttonPin = 2; // Button connected to pin 2
const int ledPin = 13; // Built-in LED
int buttonState = 0; // Variable to store button state
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Button Test - Press button to toggle LED");
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH); // Turn LED on
Serial.println("Button pressed - LED ON");
} else {
digitalWrite(ledPin, LOW); // Turn LED off
Serial.println("Button released - LED OFF");
}
delay(50); // Small delay for stability
} Potentiometers are variable resistors that provide analog input, perfect for controlling brightness, volume, or speed.
// Potentiometer Reading Example
const int potPin = A0; // Potentiometer connected to analog pin A0
const int ledPin = 9; // PWM LED connected to pin 9
int potValue = 0; // Variable to store pot reading
int ledBrightness = 0; // Variable to store LED brightness
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Potentiometer Control - Turn pot to adjust LED brightness");
}
void loop() {
potValue = analogRead(potPin);
ledBrightness = map(potValue, 0, 1023, 0, 255);
analogWrite(ledPin, ledBrightness);
Serial.print("Pot Value: ");
Serial.print(potValue);
Serial.print(" | LED Brightness: ");
Serial.println(ledBrightness);
delay(100);
} Create a sensor monitoring system that reads multiple input devices and provides real-time feedback through the Serial Monitor.