0Pricing
Arduino & IoT Academy · Lezione

Servo controllato da una manopola

Comandi il servo con un potenziometro.

Servo controllato da una manopola è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Arduino & IoT Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Arduino & IoT Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Steer with a Knob

Time to take control by hand. Turn a potentiometer and the servo follows in real time, like a tiny steering wheel. 🎛️

The Plan

The idea is simple: read the knob, convert that number into an angle, and write it to the servo, over and over inside loop().

Read the Knob

Use analogRead() on the pot's pin. It gives back a number from 0 to 1023 that follows how far you've turned the knob.

int val = analogRead(A0);

The Range Mismatch

The knob speaks 0 to 1023, but the servo only understands 0 to 180. You need to convert between those two scales.

map() to the Rescue

The map() function rescales a number from one range into another. It's the perfect bridge from knob units to servo degrees.

int angle = map(val, 0, 1023, 0, 180);

Send It to the Servo

Now feed that mapped angle straight into write(). The servo snaps to whatever position your knob is pointing at.

myServo.write(angle);

The Whole Loop

Read, map, write: stack those three lines in loop() and the servo tracks your knob continuously.

void loop() {
  int val = analogRead(A0);
  int angle = map(val, 0, 1023, 0, 180);
  myServo.write(angle);
}

Let It Settle

Add a tiny delay() at the end of the loop. It gives the servo a moment to reach each position and smooths out the motion.

delay(15);

Wire the Pot

The potentiometer's outer pins go to 5V and ground, and the middle wiper pin goes to analog pin A0. That gives a clean sweep of voltage.

Tame the Jitter

Analog readings wobble a little, so the servo may jitter. Reading several times and averaging, or ignoring tiny changes, calms it down.

Flip the Direction

Want the servo to turn the opposite way? Just swap the map output to go from 180 to 0 instead of 0 to 180.

int angle = map(val, 0, 1023, 180, 0);

Quick Check

Let's connect the knob reading to a servo angle.

Recap

Read the pot, map 0 to 1023 into 0 to 180, then write that angle each loop. Now your hand directly steers the servo. 🙌

Domande Frequenti

La lezione «Servo controllato da una manopola» è gratuita?

Sì — il testo completo di «Servo controllato da una manopola» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Arduino & IoT Academy, passa a CoddyKit PRO. Il corso Arduino & IoT Academy include 4 lezioni in totale.

Cosa imparerò in «Servo controllato da una manopola»?

Comandi il servo con un potenziometro. Eserciti Arduino & IoT Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Arduino & IoT Academy?

Non è richiesta alcuna esperienza precedente. Arduino & IoT Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Servo controllato da una manopola»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Arduino & IoT Academy?

Sì. Ogni lezione Arduino & IoT Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Come un servo conosce il proprio angolo
  2. La libreria Servo e attach
  3. Eseguire una scansione da 0 a 180
  4. Servo controllato da una manopola
← Torna a Arduino & IoT Academy