انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Maker: home
0. Lab tour1
1. The start: the Arduino6
2. Wiring and the traffic light4
3. Inputs: the button4
4. Sound and movement5
5. Sensors and numbers5
6. Distance, the screen and colours5
7. Motors and the car4
8. The project: the city's smart gate3
9. From blocks to C++ code5
10. Sensors and screens in code5
11. Smart systems5
12. Bluetooth: control from the phone1

A light that gets brighter and dimmer: analogWrite and map

Section 5: Sensors and numbersLesson 5.2 · ⏱ 9 min

analogWrite gives the LED a strength from 0 (off) to 255 (brightest), on the pins marked with ~ like 9.

The problem: the reading is from 0 to 1023, and the light from 0 to 255. The map command turns a number from one range into another.

🧩 The LED follows the potentiometer
Blocks: loop — repeats forever

📌 Another example: a light with four levels

analogWrite with four values: 0, then 85, then 170, then 255, and it repeats.

🧩 A light with four levels
Blocks: loop — repeats forever
The light gets brighter step by step
The light gets brighter step by step
✍️ Practise
Build the changing light.
✍️ Practise here in the lessonor open the lab in a new page ↗
✍️ Practise
The servo follows the potentiometer (map to 0~180).
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A steady dim light: the value 40.

👀 Show the solution
🧩 Solution
Blocks: loop — repeats forever
In the lab
In the lab

Result: dim.

Medium

The knob controls the light (map from 0–1023 to 0–255).

👀 Show the solution
🧩 Solution
Blocks: loop — repeats forever
In the lab
In the lab

Result: the more you turn the knob, the brighter the light.

Hard

Reversed: the knob at its start = the brightest light (map to 255–0).

👀 Show the solution
🧩 Solution
Blocks: loop — repeats forever
In the lab
In the lab

Result: it gets dimmer as you turn it.

Finished the lesson? Mark it so your progress counts.