Programming lesson
Arduino Mega Blink and Tone Lab: Getting Hands-On with Microcontroller Programming
A step-by-step tutorial for CSE/ECE474 Lab 1 on the Arduino Mega. Learn to install Arduino IDE, modify blink sketches, control LEDs and speakers, and use an oscilloscope for debugging.
Introduction to the Arduino Mega Lab
Welcome to the first lab of CSE/ECE474. This tutorial will guide you through setting up the Arduino Mega, writing your first sketch, and exploring outputs like LEDs and speakers. By the end, you'll also know how to use an oscilloscope to verify signals. Whether you're a beginner or need a refresher, these steps follow the official lab procedures.
Part I: Setting Up the Arduino IDE and Running Blink
Installing the Arduino IDE
First, download and install the Arduino IDE from the official website. Choose the version for your operating system (Windows, macOS, or Linux). Once installed, open the IDE.
Opening the Blink Example
Go to File > Examples > 01.Basics > Blink. This loads a simple program that turns the built-in LED on and off every second.
Breaking and Fixing Code
Delete the semicolon on line 28 and click the checkmark to compile. You'll see an error message. Add the semicolon back and compile again to confirm it works.
Connecting and Configuring the Board
Connect your Arduino Mega to your computer via USB. The green ON LED should light up. In the IDE, select Tools > Board > Arduino Mega 2560 or Mega 2650. Then choose Tools > Serial Port > [the port with (Arduino Mega)].
Uploading and Verifying
Click the right arrow (Upload). You'll see TX/RX LEDs flash rapidly as the code transfers. After upload, the built-in LED (labeled L) should blink once every two seconds.
Part II: Modifying the Blink Speed
Change the delay(1000) to delay(200) and re-upload. The LED now blinks five times faster. Unplug USB and connect the external power adapter; the Arduino retains the last program and runs it automatically.
Part III: External LED on Pin 10
Wiring the External LED
Connect a 250Ω resistor between the cathode (short leg) of an LED and a GND pin on the Arduino. Connect the anode (long leg) to digital pin 10. Use a breadboard for easy connections.
Modifying the Code
Change LED_BUILTIN to 10 in the sketch. Compile and upload. The external LED should blink in sync with the original pattern.
Part IV: Adding a Speaker and Multiple Tasks
Wiring the Speaker
Connect an 8-ohm speaker between pin 2 and +3.3V. The speaker will produce clicks when pin 2 toggles.
Code for Blinking and Clicking
Modify the sketch to blink the onboard LED (pin 13) and external LED (pin 10) alternately, and make the speaker click on each state change. Use digitalWrite to control all pins.
Part V: Continuous Tone without tone()
To generate a 250 Hz tone, toggle pin 2 with a delay of 2 ms (since period = 1/250 = 4 ms, half-period = 2 ms). Use delayMicroseconds(2000). Important: turn off the tone after a few seconds to avoid annoyance. Keep the LEDs flashing continuously.
void loop() {
digitalWrite(13, HIGH);
digitalWrite(10, LOW);
digitalWrite(2, HIGH);
delayMicroseconds(2000);
digitalWrite(2, LOW);
delayMicroseconds(2000);
// ... repeat with opposite LED states
}Part VI: Measuring with an Oscilloscope
Understanding the Oscilloscope
An oscilloscope displays voltage versus time. Key controls: Vertical (volts/division), Horizontal (time/division), and Trigger (stabilizes the waveform).
Measuring the 250 Hz Signal
Connect the probe to pin 2 and ground. Adjust time/division to 2 ms/div to see about 2.5 cycles. Use the Measure button to read frequency. Confirm it's approximately 250 Hz.
Conclusion
You've completed the Arduino Mega lab: from IDE setup to oscilloscope measurements. This foundation will help you tackle more complex projects. Remember, the key is to understand each step and experiment with modifications.