ESC Week 4 Lab
ESC Week 4 Lab
#include <Arduino.h>
// Define the pins for the built-in LED and the external LED
const int builtInLED = LED_BUILTIN; // LED_BUILTIN is a constant for the built-in LED pin (usually 13)
void setup() {
pinMode(builtInLED, OUTPUT);
pinMode(externalLED, OUTPUT);
void loop() {
// Turn on the built-in LED and turn off the external LED
digitalWrite(builtInLED, HIGH);
digitalWrite(externalLED, LOW);
// Turn off the built-in LED and turn on the external LED
digitalWrite(builtInLED, LOW);
digitalWrite(externalLED, HIGH);
1. Open the Arduino IDE and select the correct board from the "Tools" menu.
2. Load the "Blink" example code to your Arduino board to make sure it's functioning correctly.
You can find this example in the Arduino IDE under File > Examples > 01.Basics > Blink.
3. After testing the Blink example, connect an LED to Pin 9 on the Arduino board and don't
forget to use a current-limiting resistor. Make sure the longer leg (anode) of the LED is
connected to Pin 9 and the shorter leg (cathode) is connected to the resistor, and then to the
ground (GND) pin on the Arduino.
4. Modify the code provided above to alternate between the built-in LED and the external LED.
This code will turn one LED on while turning the other off, and vice versa, with a 1-second
delay between each transition.
5. Upload the modified code to your Arduino board using the Arduino IDE.
6. Provide evidence in your report, which can include pictures or videos showing both LEDs
alternately lighting up and going off as expected.
Remember to use the correct resistor to limit the current through the external LED, and ensure that
you have correctly connected the external LED to Pin 9 on the Arduino board
ACTIVITY 2
#include <Arduino.h>
// Define the pins for the button, built-in LED, and LED state variables
const int builtInLED = LED_BUILTIN; // LED_BUILTIN is a constant for the built-in LED pin (usually 13)
unsigned long previousMillis = 0; // Stores the last time the LED state was changed
const long interval = 250; // Interval at which the LED should blink (in milliseconds)
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(builtInLED, OUTPUT);
void loop() {
// Check if the button is pressed (buttonState is LOW) and enough time has passed
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
digitalWrite(builtInLED, ledState);
if (buttonState == HIGH) {
digitalWrite(builtInLED, LOW);
1. Choose a digital input pin on your Arduino (in the code above, it's set to pin 2). Connect a
button to this pin along with an external pull-down resistor. Make sure the button is
connected between the chosen pin and ground (GND).
2. Open the Arduino IDE and select the correct board from the "Tools" menu.
3. Upload the code to your Arduino board using the Arduino IDE.
4. Provide evidence in your report, which can include pictures or videos showing that when you
press the button, the built-in LED blinks every 250 ms, and when you release the button, the
built-in LED turns off.
Using the millis() function allows you to create non-blocking code that ensures the Arduino
continues to perform other tasks while monitoring the button state and blinking the LED as required
ACTIVITY 3
#include <Arduino.h>
const int analogInputPin = A0; // Change this to the appropriate analog input pin
const int builtInLED = LED_BUILTIN; // LED_BUILTIN is a constant for the built-in LED pin (usually 13)
void setup() {
Serial.begin(9600);
pinMode(builtInLED, OUTPUT);
void loop() {
Serial.println(analogValue);
} else {
// Add a small delay to reduce the rate of analog readings and Serial output
delay(100); // Adjust this value as needed for your observation
1. Choose an analog input pin on your Arduino (in the code above, it's set to A0). Connect a
potentiometer to this pin.
2. Connect the wiper of the potentiometer to the chosen analog input pin and connect one of
the outer legs to 5V (VCC) and the other to Ground (GND) for the potentiometer to function
correctly.
3. Open the Arduino IDE and select the correct board from the "Tools" menu.
4. Upload the code to your Arduino board using the Arduino IDE.
5. Open the Serial Monitor (Ctrl+Shift+M or under "Tools") to view the analog readings and
observe the LED control based on the analog value.
6. If you want to visualize the analog data in the Serial Plotter, go to "Tools" > "Serial Plotter" in
the Arduino IDE. You should see a graph displaying the analog readings.
7. Provide evidence in your report, which can include pictures, videos, or screen captures of the
Serial Monitor and Serial Plotter displaying the analog readings and LED control as described.
This code will read the analog value from the potentiometer, display it in the Serial Monitor and
Serial Plotter, and control the built-in LED based on the analog reading as specified in your activity.
ACTIVITY 4
// Transmitter Arduino
#include <Arduino.h>
const int analogInputPin = A0; // Change this to the appropriate analog input pin
const int externalLED = 9; // Change this to the appropriate external LED pin
void setup() {
pinMode(externalLED, OUTPUT);
}
void loop() {
Serial1.println(analogValue);
digitalWrite(externalLED, HIGH);
digitalWrite(externalLED, LOW);
// Receiver Arduino
#include <Arduino.h>
const int builtInLED = LED_BUILTIN; // LED_BUILTIN is a constant for the built-in LED pin (usually 13)
void setup() {
pinMode(builtInLED, OUTPUT);
void loop() {
if (Serial1.available()) {
} else {
1. Connect the transmitter Arduino to the receiver Arduino via serial communication. To do
this, connect the TX pin (Transmitter) of the transmitter Arduino to the RX pin (Receiver) of
the receiver Arduino and vice versa.
2. Connect an external LED to both Arduinos, as you did in Activity 1, with appropriate resistors.
3. To sync both Arduinos, connect the RESET pins of both boards to the same row on the
breadboard, and ground the RESET pins as needed (to reset, make sure they are connected
to ground briefly).
4. Upload the transmitter code to one Arduino and the receiver code to the other.
5. The roles of the transmitter and receiver will switch every 10 seconds. The transmitter will
send the analog reading from the potentiometer, and the receiver will turn on its built-in LED
if the received data is greater than or equal to 512.
6. Provide evidence in your report, which can include pictures, videos, or observations of the
LED behavior and data transmission between the two Arduinos as described.