Read a slider on the Arduino

FunduinoPublished on

Analoge Werte einlesen und damit eine LED steuern

Ein Schieberegler ist ein Potentiometer in anderer Bauform – statt zu drehen, schiebst du. Der Mikrocontroller liest daraus einen Wert zwischen 0 und 1023.

In diesem Einsteigerprojekt steuert dieser Wert die Blinkgeschwindigkeit der LED, die auf jedem Board fest an Pin 13 sitzt. Gleichzeitig wandert er in den Seriellen Monitor – die Grundlage dafür, später beliebige Funktionen an bestimmte Reglerstellungen zu knüpfen.

Slide potentiometer on the breadboard with the Funduino UNO R3
The experimental setup

You need this

10 kOhm Slide Potentiometer - linear, 20 mm travel, up to 12 V 10 kOhm Slide Potentiometer - linear, 20 mm travel, up to 12 V 2,90 € *Item no. F23106454About the product — 10 kOhm Slide Potentiometer - linear, 20 mm travel, up to 12 V
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm

All 4 parts together 18,66 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

How a slider works

A slide potentiometer is simply a potentiometer in a different form factor. Two of its terminals are connected to positive and negative, and the third to an analog input on the microcontroller.

Depending on how far you move the slider, this third pin outputs a voltage between 0 and 5 volts. The microcontroller reads a value between 0 and 1023. Whether the far-left position reads 0 or 1023 depends on which way around you connect positive and negative.

Build the circuit

The potentiometer's wiper connects to A0, and the two outer terminals connect to 5V and GND.

You don't need a separate LED: The program uses the LED built into every board, which is connected to pin 13.

Slide potentiometer on the breadboard with the Funduino UNO R3
The experimental setup

The program

The sketch reads the potentiometer value and uses it directly as the delay for blinking. It also sends the value to the Serial Monitor - which you'll need as soon as you want to link other functions to specific potentiometer positions.

If you slide the control all the way to the end stop, the sensor value becomes 0, and so does delay(0). The LED then blinks so fast that it appears to stay lit. This is not an error.

Control the blinking speed with the slider
int eingang= A0; // Port A0 is now equated with the word "eingang"
int sensorwert = 0; // "sensorwert" is used as a variable for the slider value
void setup()
{
pinMode (13, OUTPUT); // Pin 13 is set as an output so the LED connected there can be used
Serial.begin (9600); // Start the data connection to the serial monitor
}
void loop()
{
sensorwert =analogRead(eingang); //The slider value is read and stored in the variable "sensorwert"
Serial.println (sensorwert); // The value is sent to the serial monitor
digitalWrite (13, HIGH); // Turn on the LED on pin 13
delay (sensorwert); // LED lights up for the duration of the sensor value (in milliseconds)
digitalWrite (13, LOW); // Turn off the LED on pin 13
delay (sensorwert); // LED stays off for the duration of the sensor value (in milliseconds)
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.