0Pricing
Arduino & IoT Academy · Aula

Servo controlado por botão giratório

Comande o servo com um potenciômetro.

Servo controlado por botão giratório é uma aula grátis de Arduino & IoT Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Arduino & IoT Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Arduino & IoT Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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. 🙌

Perguntas Frequentes

A aula “Servo controlado por botão giratório” é grátis?

Sim — o texto completo de “Servo controlado por botão giratório” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Arduino & IoT Academy, atualize para CoddyKit PRO. O curso de Arduino & IoT Academy inclui 4 aulas no total.

O que vou aprender em “Servo controlado por botão giratório”?

Comande o servo com um potenciômetro. Você pratica Arduino & IoT Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Arduino & IoT Academy?

Nenhuma experiência prévia é necessária. Arduino & IoT Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Servo controlado por botão giratório”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Arduino & IoT Academy?

Sim. Cada aula de Arduino & IoT Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Como um servo conhece seu ângulo
  2. A biblioteca Servo e attach
  3. Percorra de 0 a 180
  4. Servo controlado por botão giratório
← Voltar para Arduino & IoT Academy