0% found this document useful (0 votes)
3 views34 pages

Arduino_Introduction_YA_Spring 25

The document provides an introduction to Arduino, covering its hardware, software, and the Arduino IDE. It includes objectives for practical tasks such as turning an LED on/off, working with variables, and controlling LED brightness. Additionally, it explains key functions and structures in Arduino programming, including how to use variables, control structures, and various input/output functions.

Uploaded by

Yasmeen Al-Saleh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views34 pages

Arduino_Introduction_YA_Spring 25

The document provides an introduction to Arduino, covering its hardware, software, and the Arduino IDE. It includes objectives for practical tasks such as turning an LED on/off, working with variables, and controlling LED brightness. Additionally, it explains key functions and structures in Arduino programming, including how to use variables, control structures, and various input/output functions.

Uploaded by

Yasmeen Al-Saleh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 34

Introduction of Arduino

ENG 102 Lab


Instructor: Yasmeen AlSaleh
Objectives
• What is Arduino?
• Arduino Hardware
• Arduino software
• Familiarizing Yourself With The Arduino IDE
• Variables (setup, loop)
• Control Structures
• Arduino Libraries
• Turn an LED ON/OFF
• Work with variables in Arduino Code
• Control the number of times the LED is turned ON/OFF
• Adding Text Output
Part 1: What is Arduino?

Inputs Outputs

PIR Sensor Ultrasonic Sensor Gas Sensor Servo Motor LCD

LEDs

PotentiometerPhotoresistorTemperature Pushbutton 7 Segment


Buzzer
Sensor Display
Part 1: What is Arduino?

Inputs Microcontroller Outputs


Part 2: Arduino Hardware

Classic Family Nano Family MKR Family


Part 2: Arduino Hardware
Classic Family: UNO Rev 3

Reset button:
Resets the ATmega microcontroller.

Microcontroller:
ATmega328P

https://store-usa.arduino.cc/products/arduino-uno-rev3?selectedStore=us
Part 2: Arduino Hardware
USB port:
Used for powering your Arduino UNO,
Classic Family: UNO Rev 3 uploading your sketches to your
Arduino, and for communicating with
your Arduino sketch (via Serial.
println() etc.).

Power connector:
This is how you power your Arduino
when it's not plugged into a USB port
for power. Can accept voltages
between 7-12V.

Power LED:
Indicates that your Arduino is
receiving power. Useful for debugging.
Part 2: Arduino Hardware
General Purpose Input/Output Pins
Classic Family: UNO Rev 3 GPIO
Operate at 5V, and can provide/receive 20mA

20 pins

14 Digital Pins: 6 Analog Pins:


- ON/OFF (Push button) - Operate within range of
- Input/Output values (Temp sensor)
>> pinMode() - Inputs labeled as A0 up to A5
>> digitalWrite() - 10 bits of resolution
>> digitalRead() >> analogRead()
>> analogReference()
Part 2: Arduino Hardware
Classic Family: UNO Rev 3 Pin 13 LED
The only actuator built-in to your board.
Besides being a handy target for your
first blink sketch, this LED is very useful
for debugging.

Digital Input/Output Pins:


13 pins
6 of them can be used as PWM outputs (~)

Note:
Use these pins with digitalRead(), digitalWrite(), and
analogWrite().
- analogWrite() works only on the pins with the PWM
symbol.
Part 2: Arduino Hardware
Classic Family: UNO Rev 3

Power:
GND, 3.3 V, and 5V pins Use these
pins to provide +3.3 V, +5V power, and
ground to your circuits.

Analog In:
6 input pins:
Note: Use these pins with analogRead().
Part 3: Arduino software

Arduino IDE 1.8.19 Write your code Upload your code


Part 4: Familiarizing Yourself With The Arduino IDE
Part 4: Familiarizing Yourself With The Arduino IDE
1.Open the IDE: Double-click the shortcut on your 6.Verify a sketch: Let’s check your sketch for
desktop or in your app folder. any errors by hitting the checkmark at the top
2.Find your sketch and library locations: Open up left of your sketch. You’ll see the status of your
your documents folder and look for a sub-folder sketch outlined in the black notification box at
called Arduino. This is where your sketches and the bottom of your screen.
libraries will be stored (more on libraries later). 7.Upload a sketch: Now hit the little right-
3.Open a sketch: To open a sketch, just go to pointing arrow to upload. If you look closely at
File>Examples>01.Basics>Blink. the Arduino board during this step, you’ll see
4.Connect your board: While we’re here, let’s look the TX and RX lights blinking.
at this Blink sketch in action! Grab your Arduino 8.Play with the sketch: Once the Blink sketch
board and plug it into your computer with your USB is uploaded, the built-in LED on your board will
cable. start blinking.
5.Test your board is connected: Click on
Tools>Board. Now you can select your board by
name. Now hit Tools>Port. Simply select the port
that shows the name of your board.
Language References:
How To Use Variables
A variable has four ingredients:
• Type: The kind of information that you’re actually going to store in the variable
• Name: Variable names can contain letters, numbers, and underscores, but they can’t
start with a number.
• Assignment operator: When you initialize a variable, you’ll see an equals sign to signal
that the type and name apply to whatever information the sketch includes next.
• Value: finally, there’s the value you assign to the variable.
How To Use Control Structures
control structures allow you to adjust the flow of code, and that
paves the way for your Arduino to produce more sophisticated and
complex outcomes.
if else switch...case

Syntax Syntax Syntax


if (condition) { if (condition1) { switch (var) {
//statement(s) // do Thing A
} }
case label1:
else if (condition2) { // statements
// do Thing B break;
} case label2:
else { // statements
// do Thing C break;
} default:
// statements
break; }
Arduino Functions (PinMode())
• The pinMode() function in Arduino is used to configure a specific pin as either
an input, output. It determines how the pin behaves in a circuit.
•pin: The pin number you want to configure.
•mode: Can be one of the following:
•INPUT – Configures the pin to read signals (e.g., from a sensor or button).
•OUTPUT – Configures the pin to send signals (e.g., to an LED or motor).
Arduino Functions (DigitalWrite())
• The digitalWrite() function in Arduino is used to set a digital pin to
HIGH (1) or LOW (0) when it is configured as an OUTPUT.

• pin: The pin number you want to


control.
• value:
• HIGH (1)
• LOW (0)
Example: Blink an LED
Arduino Functions (DigitalRead())
• The digitalRead() function in Arduino is used to read the state (HIGH or
LOW) of a digital input pin.

•Pin: The pin number to read from.


•Returns:
•HIGH (1) – If the pin is receiving voltage (e.g., button pressed).
•LOW (0) – If the pin is at ground (e.g., button not pressed).
Example: Reading a Button Press (Digital Read ())
Arduino Functions (AnalogRead())
• The analogRead() function in Arduino is used to read the analog voltage
from an analog input pin (A0 to A5 on most boards) and convert it into a
digital value. It is commonly used for reading sensors like temperature
sensors, potentiometers, and light sensors.

•pin: The analog pin (A0, A1, A2, etc.) from which to read the value.
•Returns: An integer between 0 and 1023 (for 10-bit ADC on most Arduino
boards).
•0 → 0V
•1023 → 5V (or 3.3V on some boards)
Basic Code to Read Temperature
void setup() {
Serial.begin(9600); // Start serial monitor
}

void loop() {
int sensorValue = analogRead(A0); // Read analog value from LM35
float temperatureC = sensorValue * 0.488; // Convert to °C (simplified formula)

Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");

delay(1000); // Wait 1 second


}
Arduino Functions (AnalogWrite())
• The analogWrite() function is used to output a PWM (Pulse Width
Modulation) signal on PWM-capable pins (marked with ~ on most Arduino
boards).

•pin: The PWM pin (3, 5, 6, 9, 10, or 11 on most Arduinos).


•value: A number from 0 to 255, where:
•0 → Always OFF (0% duty cycle)
•255 → Always ON (100% duty cycle)
•127 → 50% brightness (for an LED)
Example: Adjust LED Brightness
Experiment 2: Turn ON/OFF an LED using Arduino
Task 1: Circuit 1: Turn ON/OFF an LED using Arduino

NOTE: positive anode + (bent wire) should be on the


resistor side and the negative cathode - (straight wire)
should be leading towards the – on the board.
Arduino Program Structure
void setup()
- Called once
- Used to initialize the pin
modes and start the serial
communication
Note: it has to be included
even if there are no statements

void loop()
- Execute the set of statements
enclosed in the curly brackets
repeatedly.
- Used to read inputs, trigger
outputs, check conditions
Task 1: Turn ON/OFF an LED using Arduino

Note: time is measured in millisecond


Task 2: Variables in Arduino Code

4 places where variables can be


declared:
• Before void setup():
• In the void setup():
• Above void loop():
• In the void loop():

• int = integer numbers (positive and negative)


• double or float = decimal numbers (positive and negative)
• char = character (one letter / symbol / digit)
• string = 286 consecutive characters
Task 3: Control the Number of Times the LED
is turned ON/OFF
Task 3: Control the Number of Times the LED
is turned ON/OFF
Task 4: Adding Text Output
Task 5: LED Brightness Control
Objective:
• You will build upon the work you completed in Task 1.
• LED2 (Brightness Control): Connect a second LED to a different pin on the
Arduino board. Use analogWrite() to control the brightness of LED2.

Results:
•LED1 should blink continuously.
•LED2's brightness should gradually increase and decrease, creating a fading effect.
•Make sure to comment your code explaining the logic behind each section.

You might also like