0% found this document useful (0 votes)
40 views

ESC Week 4 Lab

The document describes four Arduino activities: 1) Blinking LEDs, 2) Button-controlled blinking, 3) Potentiometer-controlled LED, and 4) Wireless LED control. For each activity, code is provided along with instructions for wiring circuits and uploading code to an Arduino board.

Uploaded by

sholyfila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

ESC Week 4 Lab

The document describes four Arduino activities: 1) Blinking LEDs, 2) Button-controlled blinking, 3) Potentiometer-controlled LED, and 4) Wireless LED control. For each activity, code is provided along with instructions for wiring circuits and uploading code to an Arduino board.

Uploaded by

sholyfila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

// Include the necessary Arduino library

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

const int externalLED = 9; // You can change this pin as needed

void setup() {

// Initialize the LED pins as outputs

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

delay(1000); // Delay for 1 second (1000 milliseconds)

// Turn off the built-in LED and turn on the external LED

digitalWrite(builtInLED, LOW);

digitalWrite(externalLED, HIGH);

delay(1000); // Delay for 1 second

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 the necessary Arduino library

#include <Arduino.h>

// Define the pins for the button, built-in LED, and LED state variables

const int buttonPin = 2; // Change this to the appropriate button pin

const int builtInLED = LED_BUILTIN; // LED_BUILTIN is a constant for the built-in LED pin (usually 13)

int ledState = LOW; // Stores the LED state (ON or OFF)

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() {

// Initialize the button pin as an input with an internal pull-up resistor

pinMode(buttonPin, INPUT_PULLUP);

// Initialize the LED pin as an output

pinMode(builtInLED, OUTPUT);

void loop() {

// Read the state of the button

int buttonState = digitalRead(buttonPin);

// Get the current time


unsigned long currentMillis = millis();

// Check if the button is pressed (buttonState is LOW) and enough time has passed

if (buttonState == LOW && currentMillis - previousMillis >= interval) {

// Save the last time the LED state was changed

previousMillis = currentMillis;

// Toggle the LED state

if (ledState == LOW) {

ledState = HIGH;

} else {

ledState = LOW;

// Update the LED state

digitalWrite(builtInLED, ledState);

// If the button is released, turn off the LED

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 the necessary Arduino library

#include <Arduino.h>

// Define the analog input pin and built-in LED pin

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() {

// Initialize the Serial communication

Serial.begin(9600);

// Initialize the LED pin as an output

pinMode(builtInLED, OUTPUT);

void loop() {

// Read the analog value from the potentiometer

int analogValue = analogRead(analogInputPin);

// Print the analog value to the Serial Monitor

Serial.println(analogValue);

// Control the built-in LED based on the analog reading

if (analogValue < 512) {

digitalWrite(builtInLED, LOW); // Turn off the LED

} else {

digitalWrite(builtInLED, HIGH); // Turn on the LED

// 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>

// Define the analog input pin and external LED pin

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() {

// Initialize the HardwareSerial communication

Serial1.begin(9600); // Use Serial1 for communication

// Initialize the LED pin as an output

pinMode(externalLED, OUTPUT);

}
void loop() {

// Read the analog value from the potentiometer

int analogValue = analogRead(analogInputPin);

// Send the analog value to the receiver Arduino

Serial1.println(analogValue);

// Turn on the external LED

digitalWrite(externalLED, HIGH);

delay(10000); // Transmit for 10 seconds

// Turn off the external LED

digitalWrite(externalLED, LOW);

delay(10); // Small delay before switching roles

// Receiver Arduino

#include <Arduino.h>

// Define the built-in LED pin

const int builtInLED = LED_BUILTIN; // LED_BUILTIN is a constant for the built-in LED pin (usually 13)

void setup() {

// Initialize the HardwareSerial communication

Serial1.begin(9600); // Use Serial1 for communication

// Initialize the LED pin as an output

pinMode(builtInLED, OUTPUT);

void loop() {
if (Serial1.available()) {

// Read the analog value received from the transmitter Arduino

int receivedValue = Serial1.parseInt();

// Control the built-in LED based on the received analog reading

if (receivedValue >= 512) {

digitalWrite(builtInLED, HIGH); // Turn on the LED

} else {

digitalWrite(builtInLED, LOW); // Turn off the LED

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.

You might also like