πŸ€–

Jeremy's Arduino Lab

Program the Mega2560 β€” from your first blink to a smart night light

πŸ“Ÿ Which device do I use?

πŸ’» Windows computer β€” for coding

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

πŸ“± iPad β€” for reading & watching

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

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.
What you need
πŸ–₯️ Windows computer πŸ”Œ Mega2560 board πŸ”΅ Blue USB cable (square end)
Install Arduino IDE

On the Windows computer, click one of these β€” the Microsoft Store version is the easiest:

  1. 1
    Microsoft Store β†’ press Install.  Installer (.exe) β†’ open the downloaded file and click I Agree β†’ Install, then Finish.
  2. 2
    Open Arduino IDE (it may take a minute the first time).
  3. 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.
  4. 4
    In Arduino IDE: Tools β†’ Board β†’ Arduino AVR Boards β†’ Arduino Mega or Mega 2560. If it offers to install β€œArduino AVR Boards”, click Install.
  5. 5
    Tools β†’ Port β†’ pick the port that appeared (something like COM3).
πŸ› οΈ No COM port showing up?
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.
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 ✨ That's it!
Wiring
No wiring needed β€” the Mega2560 has a small LED built in, already wired to pin 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. 2
    Click the round β†’ Upload button (top-left). Wait for β€œDone uploading.”
  3. 3
    Watch the board β€” the little LED blinks on for 1 second, off for 1 second. πŸŽ‰
πŸ”¬ 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).
πŸ§ͺ 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.
2

A Button Switch

β˜…β˜… Input + Output
🎯 Wire up a real LED and a push button. Press the button β†’ light ON. Let go β†’ light OFF.
Parts Needed
🍞 Breadboard πŸ’‘ LED Γ— 1 🟫 220Ξ© resistor (red-red-brown) πŸ”˜ 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)
+βˆ’ βˆ’+ β†’ 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.

πŸ”Œ 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.
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.
πŸ§ͺ 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
🎯 Make the LED smoothly fade brighter and dimmer, like it's slowly breathing.
Parts Needed
🍞 Breadboard πŸ’‘ LED Γ— 1 🟫 220Ξ© resistor πŸ”— Jumper wires
Wiring
Pin 9 ~ 220Ξ© LED GND ⚑ use a pin with a "~" β€” only those can fade!
🍞 Show realistic breadboard layout (Fritzing-style)
+βˆ’ βˆ’+ β†’ 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.

πŸ”Œ 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.
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.
πŸ§ͺ 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.
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.
Parts Needed
🍞 Breadboard πŸŒ— Photoresistor (light sensor) 🟫 10kΞ© resistor (brown-black-orange) πŸ’‘ LED + 220Ξ© resistor πŸ”— 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)
+βˆ’ βˆ’+ 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.

πŸ”Œ 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.
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. 2
    Cover the sensor with your hand β€” the number drops and the LED gets brighter.
  3. 3
    Shine a phone light on it β€” the number rises and the LED dims.
πŸ”¬ 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.
πŸ§ͺ 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.

⚠️ Lab Rules

πŸ† My Progress

#ExperimentLevelDone?
0Get Set Upβ€”
1Make the Light Blinkβ˜…
2A Button Switchβ˜…β˜…
3Breathing Lightβ˜…β˜…β˜…
4Smart Night Lightβ˜…β˜…β˜…β˜