🤖

Jeremy's Arduino Lab

Jeremy 的 Arduino 实验室

Program the Mega2560 — from your first blink to a smart night light

给 Mega2560 写代码 —— 从你的第一次闪烁,到一盏聪明的夜灯

📟 Which device do I use?

📟 我该用哪台设备?

💻 Windows computer — for coding

💻 Windows 电脑 —— 用来编程

The Arduino IDE program and the USB cable only work on a real computer. You'll write code and run every experiment here.

Arduino IDE 软件和 USB 线只能在真正的电脑上用。你会在这里写代码、做每一个实验。

📱 iPad — for reading & watching

📱 iPad —— 用来看页面和视频

Use the iPad to read this page and watch the ELEGOO videos while you build. (It can't run the Arduino IDE or plug into the board.)

边搭电路边用 iPad 看这个页面、看 ELEGOO 的视频。(iPad 不能运行 Arduino IDE,也插不了开发板。)

0

Get Set Up

准备工作

Do this once 只需做一次
🎯 Install the Arduino IDE on your Windows computer and make sure it can talk to your Mega2560 board.
🎯 在 Windows 电脑上安装 Arduino IDE,并确认它能和你的 Mega2560 开发板通信。
What you need
所需材料
🖥️ Windows computer 🖥️ Windows 电脑 🔌 Mega2560 board 🔌 Mega2560 开发板 🔵 Blue USB cable (square end) 🔵 蓝色 USB 线(方头那端)
Install Arduino IDE
安装 Arduino IDE

On the Windows computer, click one of these — the Microsoft Store version is the easiest:

Windows 电脑上,点下面任意一个 —— Microsoft Store 版本最简单:

  1. 1
    Microsoft Store → press Install.  Installer (.exe) → open the downloaded file and click I Agree → Install, then Finish.
  2. 1
    Microsoft Store → 点 Install(安装)。 安装包 (.exe) → 打开下载好的文件,点 I Agree(我同意)→ Install(安装),最后点 Finish(完成)
  3. 2
    Open Arduino IDE (it may take a minute the first time).
  4. 2
    打开 Arduino IDE(第一次打开可能要等一会儿)。
  5. 3
    Plug the board into the computer: the square (USB-B) end into the board, the flat (USB-A) end into the computer. A little green power light turns on.
  6. 3
    把开发板插到电脑上:方头(USB-B)那端插进开发板,扁头(USB-A)那端插进电脑。一个小小的绿色电源灯会亮起来。
  7. 4
    In Arduino IDE: Tools → Board → Arduino AVR Boards → Arduino Mega or Mega 2560. If it offers to install “Arduino AVR Boards”, click Install.
  8. 4
    在 Arduino IDE 里:Tools → Board → Arduino AVR Boards → Arduino Mega or Mega 2560。如果它提示要安装 “Arduino AVR Boards”,就点 Install(安装)
  9. 5
    Tools → Port → pick the port that appeared (something like COM3).
  10. 5
    Tools → Port → 选刚刚出现的那个端口(类似 COM3)。
🛠️ No COM port showing up?
🛠️ 没有出现 COM 端口?
Most ELEGOO boards use a chip called CH340 that Windows needs a small driver for. Find the CH340 driver in your kit's download folder (or search “CH340 driver Windows”), install it, then unplug and replug the board and check Tools → Port again. 大多数 ELEGOO 开发板用的是一颗叫 CH340 的芯片,Windows 需要一个小驱动才能认出它。在套件的下载文件夹里找到 CH340 驱动(或者搜索 “CH340 driver Windows”),装好它,然后拔下再插上开发板,再去看一下 Tools → Port
1

Make the Light Blink

让小灯闪烁

Your first program 你的第一个程序
🎯 Upload your very first program and make the tiny built-in light blink — then make it blink your way.
🎯 上传你的第一个程序,让板载小灯闪起来 —— 然后再让它按的方式闪。
Parts Needed
所需材料
🔌 Mega2560 + USB cable 🔌 Mega2560 + USB 线 ✨ That's it! ✨ 就这些!
Wiring
接线
No wiring needed — the Mega2560 has a small LED built in, already wired to pin 13.
不需要接线 —— Mega2560 自带一个小 LED,已经连到 引脚 13 上了。
The Code
程序代码
// Experiment 1 — Blink
// Pin 13 has a tiny LED soldered onto the board.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);   // get pin 13 ready to send power
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH); // LED ON  (HIGH = 5 volts)
  delay(1000);                     // wait 1000 ms = 1 second
  digitalWrite(LED_BUILTIN, LOW);  // LED OFF (LOW = 0 volts)
  delay(1000);                     // wait 1 second
}
  1. 1
    In Arduino IDE, delete anything in the editor and type (or paste) the code above.
  2. 1
    在 Arduino IDE 里,删掉编辑器里原有的内容,把上面的代码打上去(或粘贴进去)。
  3. 2
    Click the round → Upload button (top-left). Wait for “Done uploading.”
  4. 2
    点左上角圆形的 → Upload(上传)按钮,等到出现 “Done uploading.(上传完成)”
  5. 3
    Watch the board — the little LED blinks on for 1 second, off for 1 second. 🎉
  6. 3
    看着开发板 —— 小 LED 会亮 1 秒、灭 1 秒地闪起来。🎉
🔬 How it works
🔬 原理
setup() runs once when the board powers on. loop() runs over and over forever. digitalWrite turns a pin ON (HIGH) or OFF (LOW), and delay pauses in milliseconds (1000 = one second). setup() 在开发板通电时只运行一次loop()一遍又一遍地永远重复digitalWrite 把一个引脚打开(HIGH)或关闭(LOW),delay 则按毫秒暂停(1000 = 一秒)。
🧪 Try this
🧪 想一想
Change both 1000s to 200 and upload again — fast blink! Then make an SOS signal: 3 quick blinks, 3 slow blinks, 3 quick blinks. 把两个 1000 都改成 200 再上传一次 —— 快速闪烁!然后做一个 SOS 信号:闪 3 下快的、3 下慢的、再 3 下快的。
2

A Button Switch

按钮开关

★★ Input + Output 输入 + 输出
🎯 Wire up a real LED and a push button. Press the button → light ON. Let go → light OFF.
🎯 接上一个真正的 LED 和一个按钮。按下按钮 → 灯亮。松手 → 灯灭。
Parts Needed
所需材料
🍞 Breadboard 🍞 面包板 💡 LED × 1 💡 LED × 1 🟫 220Ω resistor (red-red-brown) 🟫 220Ω 电阻(红-红-棕) 🔘 Push button 🔘 按钮 🔗 Jumper wires 🔗 跳线
Wiring
接线
Pin 8 220Ω LED Pin 2 button GND both wires share the GND pin
🍞 Show realistic breadboard layout (Fritzing-style)🍞 显示真实面包板接线图(Fritzing 风格)
+ + → Pin 8 → Pin 2 → GND 220Ω LED+ button

Holes in the same column (within one half) are joined inside the board. The button bridges the center gap; both green jumpers go to the blue (−) GND rail.

同一列(在同一半内)的孔在板子内部是连通的。按钮跨在中间的沟上;两根绿色跳线都接到蓝色(−)GND 电源轨。

🔌 Wiring in words
🔌 用文字说接线
LED: Pin 8 → 220Ω resistor → LED long leg (+); LED short leg (−) → GND.
Button: one leg → Pin 2; the leg diagonally across → GND.
LED:引脚 8 → 220Ω 电阻 → LED 长腿(+);LED 短腿(−) → GND。
按钮:一只脚 → 引脚 2;斜对角的那只脚 → GND。
The Code
程序代码
// Experiment 2 — Button switch
const int buttonPin = 2;   // the push button
const int ledPin    = 8;   // the LED

void setup() {
  pinMode(buttonPin, INPUT_PULLUP); // built-in resistor; pressed = LOW
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if (digitalRead(buttonPin) == LOW) { // button is pressed
    digitalWrite(ledPin, HIGH);        // light ON
  } else {
    digitalWrite(ledPin, LOW);         // light OFF
  }
}
🔬 How it works
🔬 原理
digitalRead checks a pin instead of powering it. INPUT_PULLUP uses a resistor inside the chip so the pin reads HIGH normally, and LOW when you press the button (which connects it to GND). The if / else makes a decision every loop. digitalRead 是去读取一个引脚,而不是给它供电。INPUT_PULLUP 用了芯片内部的一个电阻,所以引脚平时读出来是 HIGH,按下按钮(把它连到 GND)时就变成 LOW。if / else 会在每一轮循环里做一次判断。
🧪 Try this
🧪 想一想
Turn it into a toggle: press once to turn on, press again to turn off. (Hint: you'll need a variable that remembers if the light is on, and you only flip it the moment the button is first pressed.) 把它改成一个开关切换:按一下打开,再按一下关掉。(提示:你需要一个变量来记住灯现在是不是亮的,而且只在按钮“刚被按下的那一刻”去翻转它。)
3

Breathing Light

呼吸灯

★★★ PWM & loops PWM 与循环
🎯 Make the LED smoothly fade brighter and dimmer, like it's slowly breathing.
🎯 让 LED 平滑地由亮变暗、由暗变亮,就像它在慢慢呼吸一样。
Parts Needed
所需材料
🍞 Breadboard 🍞 面包板 💡 LED × 1 💡 LED × 1 🟫 220Ω resistor 🟫 220Ω 电阻 🔗 Jumper wires 🔗 跳线
Wiring
接线
Pin 9 ~ 220Ω LED GND ⚡ use a pin with a "~" — only those can fade!
🍞 Show realistic breadboard layout (Fritzing-style)🍞 显示真实面包板接线图(Fritzing 风格)
+ + → Pin 9 ~ → GND 220Ω LED+

Same single LED branch as Experiment 2 — just move the orange wire to Pin 9 (~) so it can fade. The green jumper goes to the blue (−) GND rail.

和实验 2 一样的单个 LED 支路 —— 只要把橙色那根线挪到 引脚 9 (~),它就能渐变了。绿色跳线接到蓝色(−)GND 电源轨。

🔌 Wiring in words
🔌 用文字说接线
Same as before, but the LED's resistor now connects to Pin 9 (a pin with a ~ next to it). LED short leg (−) → GND. 和之前一样,只是 LED 的电阻现在接到 引脚 9(旁边带 ~ 符号的那种引脚)。LED 短腿(−)→ GND。
The Code
程序代码
// Experiment 3 — Breathing light (PWM)
const int ledPin = 9;   // pin 9 has a "~" (can fade)

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // fade up: 0 (off) to 255 (full brightness)
  for (int b = 0; b <= 255; b++) {
    analogWrite(ledPin, b);
    delay(6);
  }
  // fade down: 255 back to 0
  for (int b = 255; b >= 0; b--) {
    analogWrite(ledPin, b);
    delay(6);
  }
}
🔬 How it works
🔬 原理
analogWrite uses a trick called PWM: it flicks the pin on and off super fast, so the LED looks dimmer or brighter — any value from 0 to 255. A for loop counts for you, so you don't write 256 separate lines. analogWrite 用了一个叫 PWM 的小窍门:它把引脚超快地开开关关,让 LED 看起来更暗或更亮 —— 取值可以是 0 到 255 之间任意一个数。for 循环会帮你数数,这样你就不用写 256 行了。
🧪 Try this
🧪 想一想
Make the delay(6) bigger for slow, sleepy breathing or smaller for fast. Bonus: add a second LED on another ~ pin that fades the opposite way. delay(6) 改大,呼吸就变得慢悠悠、懒洋洋;改小就变快。进阶挑战:在另一个 ~ 引脚上再加一个 LED,让它按相反的方向渐变。
4

Smart Night Light

聪明的夜灯

★★★★ Sensors & Serial Monitor 传感器与串口监视器
🎯 Use a light sensor so the LED brightens when the room gets dark and dims when it's bright — all by itself. Then watch the numbers live on screen.
🎯 用一个光敏电阻,让 LED 在房间变暗时自动变亮、变亮时自动变暗 —— 全靠它自己。然后在屏幕上实时看这些数字。
Parts Needed
所需材料
🍞 Breadboard 🍞 面包板 🌗 Photoresistor (light sensor) 🌗 光敏电阻(光线传感器) 🟫 10kΩ resistor (brown-black-orange) 🟫 10kΩ 电阻(棕-黑-橙) 💡 LED + 220Ω resistor 💡 LED + 220Ω 电阻 🔗 Jumper wires 🔗 跳线
Wiring
接线
5V light sensor A0 10kΩ Pin 9 ~ 220Ω LED GND A0 reads the voltage between the sensor and the 10kΩ resistor
🍞 Show realistic breadboard layout (Fritzing-style)🍞 显示真实面包板接线图(Fritzing 风格)
+ + LDR 10kΩ 220Ω LED+ → 5V → Pin 9 ~ → A0 → GND

Left = the light-sensor divider (5V → LDR → node → 10kΩ → GND), with A0 reading the node. Right = the same LED branch as before on Pin 9 (~). Green jumpers and the black wire all meet at the blue (−) GND rail.

左边 = 光线传感器分压电路(5V → 光敏电阻 → 中间节点 → 10kΩ → GND),由 A0 读取这个节点。右边 = 和之前一样、接在 引脚 9 (~) 上的 LED 支路。绿色跳线和黑色那根线都汇集到蓝色(−)GND 电源轨。

🔌 Wiring in words
🔌 用文字说接线
Sensor: 5V → photoresistor → row we'll call “middle”; from “middle” → 10kΩ resistor → GND; and a wire from “middle” → A0.
LED: Pin 9 → 220Ω → LED long leg (+); LED short leg (−) → GND.
传感器:5V → 光敏电阻 → 我们叫它“中间”的那一行;从“中间” → 10kΩ 电阻 → GND;再从“中间”引一根线 → A0
LED:引脚 9 → 220Ω → LED 长腿(+);LED 短腿(−)→ GND。
The Code
程序代码
// Experiment 4 — Smart night light
const int sensorPin = A0;   // the light sensor
const int ledPin    = 9;    // LED on a "~" pin

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);       // open a line to the computer
}

void loop() {
  int light = analogRead(sensorPin);            // 0 = dark ... 1023 = bright
  int brightness = map(light, 0, 1023, 255, 0); // darker -> brighter LED
  analogWrite(ledPin, brightness);
  Serial.println(light);    // print the reading
  delay(50);
}
  1. 1
    Upload the code, then open Tools → Serial Monitor and set it to 9600 baud (bottom-right).
  2. 1
    上传代码,然后打开 Tools → Serial Monitor(串口监视器),把它设成 9600 baud(波特率)(在右下角)。
  3. 2
    Cover the sensor with your hand — the number drops and the LED gets brighter.
  4. 2
    用手盖住传感器 —— 数字会变小,LED 会变得更亮
  5. 3
    Shine a phone light on it — the number rises and the LED dims.
  6. 3
    手机的灯照它 —— 数字会变大,LED 会变暗
🔬 How it works
🔬 原理
analogRead turns the sensor's voltage into a number from 0 to 1023. map rescales one range to another (here, flipping it so dark = bright LED). Serial.println sends data back to the computer so you can see what the board senses. analogRead 把传感器的电压变成一个 0 到 1023 之间的数字。map 把一个范围重新缩放到另一个范围(这里是把它翻转过来,让“暗 = LED 亮”)。Serial.println 把数据传回电脑,这样你就能看到开发板感知到了什么。
🧪 Try this
🧪 想一想
Watch your room's numbers, pick a “dark” value, then add an if so the LED only switches on below that number — a real automatic night light that ignores small changes. 观察一下你房间里的数字,挑一个代表“暗”的数值,然后加一个 if,让 LED 只在数字低于这个值时才点亮 —— 一盏真正的、能忽略小变化的自动夜灯。

⚠️ Lab Rules

⚠️ 安全须知

🏆 My Progress

🏆 我的进度

#ExperimentLevelDone?
#实验难度完成?
0Get Set Up
0准备工作
1Make the Light Blink
1让小灯闪烁
2A Button Switch★★
2按钮开关★★
3Breathing Light★★★
3呼吸灯★★★
4Smart Night Light★★★★
4聪明的夜灯★★★★