Learning to code for robots: a six-week path
A six-week path from blinking an LED to reading a sensor, one concept per week, with code, a sensor-reading calculation and tips for debugging.
In short
Most people who try to learn robot programming begin with a long course on a language and quit before anything moves. Robots reward the opposite order: make a light blink first, and learn the language as you need it. This path takes six weeks at about three or four hours a week, and ends with a program that reacts to the world through a sensor.
In short
- Learn one concept per week: output, input, variables and loops, analog values, serial output, then decisions based on a sensor.
- Each week ends with something you build and test on a real board, not just something you read.
- A sensor reading is a number you convert to a real quantity, and checking that conversion by hand catches most early mistakes.
Who this is for
You are new to robotics, and maybe new to programming. You have, or will buy, a beginner microcontroller board (an Arduino-style board is a common choice), a USB cable, a breadboard, a few LEDs and resistors, a push button and a simple sensor. Check current prices and specifications on the seller's page.
The plan at a glance
- Week 1: output. Blink an LED.
- Week 2: input. Read a button.
- Week 3: variables and loops. Count presses and blink that many times.
- Week 4: analog output. Fade an LED.
- Week 5: serial monitor and analog input. Print a sensor value.
- Week 6: decisions. Turn something on when a reading crosses a threshold.
Step by step
1. Keep one concept per week
Resist the urge to jump ahead. If you finish early, repeat the week's project in a new way, such as a different blink pattern, rather than adding a new idea.
2. Build and test every week
Finish each week by running the project on the board. A program you have only read has not taught you what it does.
3. Change one thing at a time
When something breaks, undo your last change. When it works, change one number and predict what will happen before you run it.
4. Keep a notebook
Write down the pin numbers you used, what you expected and what happened. Next week you will not remember.
5. Use low-voltage power only
Power the board from USB or a small battery pack, and never connect it to mains power.
Worked example: six weekly projects
Weeks 1 and 2: blink and button
Week 1 teaches pinMode, digitalWrite and delay. Week 2 adds digitalRead, so a program can ask a question and answer it.
const int LED = 13;
const int BUTTON = 2;
void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
}
void loop() {
// with INPUT_PULLUP, a pressed button reads LOW
if (digitalRead(BUTTON) == LOW) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}Weeks 3 and 4: counting and fading
In week 3, store a count in a variable and use a for loop to blink that many times. In week 4, use analogWrite(pin, value) with a value from 0 to 255. On many boards this is pulse-width modulation: the pin switches on and off quickly, and the value sets how long it is on in each cycle. A value of 128 is about half on, since 128 ÷ 255 = 0.50.
Weeks 5 and 6: read a sensor and decide
A common analog input converts a voltage between 0 and 5 V into a whole number from 0 to 1023 (a 10-bit reading, which is 1024 steps). Print it with Serial.println, then convert it back to volts by hand to be sure you understand it.
const int SENSOR = A0;
const int LED = 13;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
int raw = analogRead(SENSOR);
float volts = raw * 5.0 / 1023.0;
Serial.print(raw);
Serial.print(" -> ");
Serial.println(volts);
digitalWrite(LED, raw > 600 ? HIGH : LOW);
delay(200);
}Check the conversion by hand. A reading of 512 gives 512 × 5 ÷ 1023 = 2.50 V, almost exactly halfway, as it should be. A reading of 600 gives 600 × 5 ÷ 1023 = 2.93 V, so the LED lights when the sensor voltage is above about 2.9 V. That is the first real robot behavior: a measured number controls an action. A light sensor, a potentiometer or a distance sensor can all stand in as the input, as long as you check the sensor's own documentation for its voltage range.
If something goes wrong
- The upload fails. Check the selected board and port in your editor, try another USB cable and unplug other devices.
- The LED never lights. LEDs only work one way round. Flip it, and check that it has a resistor in series.
- The button seems to flicker. Mechanical switches bounce. Add a short delay after a press, or read it only when the state changes.
- The sensor value is stuck at 0 or 1023. Check the wiring of power, ground and signal, and whether the sensor really outputs a voltage in range.
- You are stuck for over an hour. Make the smallest program that shows the problem and ask a person or a forum with that code attached.
Keep it going
Before moving on, reread your notebook and pick the project you found hardest. Redo it from a blank file without looking, which shows you what has stuck and what has not. After week 6, join your sensor to a robot: drive a motor when the reading crosses a line, which is how a robot stops before a wall. Read about electronics basics so you can pick resistors with confidence, and about building your first robot in a weekend. If you hit an error message you do not understand, you can ask DuckyHelper out loud while you keep the editor open, and it can see the code on your screen.
Frequently asked questions
- Which language should I start with?
- Use whichever your board's beginner tutorials use. The concepts of output, input, variables, loops and decisions are the same in the others.
- What if I miss a week?
- Repeat the last project quickly before continuing. The path is meant to be flexible, not a deadline.