Teach Remote lab lessons

Teach lesson

Basic Arduino 4/4: build a mini-controller

Students combine a potentiometer, servo, and LED to design a mini-controller with analog input, a decision, and visible output.

  • Arduino Board (code)
  • 40 min
  • Lower secondary / first physical-programming lessons
  • English
  • Embedded systems
Arduino Board (code)
Arduino Board (code)

Learning Outcomes

  • Combine analog input, conditionals, and two outputs.

  • Use the Servo library in a simple program.

  • Explain the behavior of a programmed system.

Student activity preview

Activity Content

Preview only. In a class session, students can fill in responses and submit their work to the teacher.

1

Design before touching the code

8 min

You have three pieces: potentiometer 1, a servo, and LED 1. In this project, the potentiometer will control the servo position, and the LED will act as a warning when the servo passes a certain angle.

A servo is a small motor designed to move to a requested position, not just spin continuously. In this project you will send it an angle from 0 to 180 degrees. The <Servo.h> library generates the timed signals the servo understands: Servo servo; creates the control object, servo.attach(3) connects it to pin 3, and servo.write(angle) asks it to move to that angle.

The servo angle goes from 0 to 180 degrees. A low threshold such as 60 degrees would turn the LED on early, near the first third of the movement. A threshold of 90 degrees would turn it on halfway through. A higher threshold such as 120 degrees would turn it on only when the servo is already in the final third of its movement. Before programming it, choose the threshold that would make the LED useful for the kind of warning you want.

At what angle would you turn the LED on: 60, 90, 120 degrees, or another value? Explain why that threshold would be useful.

2

Program the controller

22 min

Now compare your idea with this solution: the potentiometer controls the servo angle from 0 to 180 degrees, and LED 1 turns on when the angle goes above 90 degrees. Upload the program. Then change the condition so the LED turns on only from 120 degrees onward. The key line is in the if: do not change the LED pin or the servo pin unless you know why.

#include <Servo.h>

const int pot1 = A0;
const int servoPin = 3;
const int led1 = 8;

Servo servo;

void setup() {
  pinMode(led1, OUTPUT);
  servo.attach(servoPin);
}

void loop() {
  int reading = analogRead(pot1);
  int angle = map(reading, 0, 1023, 0, 180);

  servo.write(angle);

  if (angle > 90) {
    digitalWrite(led1, HIGH);
  } else {
    digitalWrite(led1, LOW);
  }

  delay(20);
}
  1. Open the Arduino lab.

  2. Upload the base program and observe the servo.

  3. Turn potentiometer 1 slowly.

  4. Change angle > 90 to angle > 120 and upload again.

  5. Save before capturing the code.

Submit your code

Capture the final code with the 120-degree threshold. Before attaching it, save main.ino in the lab environment. The snapshot must show the program you actually want your teacher to review.

3

Wrap up the project

10 min

Explain the system using these three words: input, decision, and output. You can include what you would improve if you had more time.