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

arduino session 2 esraa

The document outlines tasks related to controlling LEDs using Arduino, including code examples for blinking LEDs at different intervals and turning on an LED with a push button. It discusses the use of switches, pull-up and pull-down resistors, and introduces concepts like switch bouncing and debouncing. Additionally, it explains the Analog to Digital Converter (ADC) in microcontrollers, particularly in Arduino, which allows the conversion of analog signals to digital values.

Uploaded by

anasmostafa801
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

arduino session 2 esraa

The document outlines tasks related to controlling LEDs using Arduino, including code examples for blinking LEDs at different intervals and turning on an LED with a push button. It discusses the use of switches, pull-up and pull-down resistors, and introduces concepts like switch bouncing and debouncing. Additionally, it explains the Analog to Digital Converter (ADC) in microcontrollers, particularly in Arduino, which allows the conversion of analog signals to digital values.

Uploaded by

anasmostafa801
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Prepared by:

Esraa taha ali


First task
The first task is to blinking led every one •
second
and to blinking another led every two •
second
Code
int led1=10;
int led2=11;
void setup() {
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);}
void loop() {
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
delay(1000);
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
delay(1000);
digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
delay(1000);
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
delay(1000);
}
Proteus design
switch
Switch is an input device used to control •
ouput device
Types of switches
DIP SWITH
PUSH BUTTON
Pull up and pull down resistor
Pull up resistor Pull down resistor
Pull up is used to prevent floating • Pull down is used to prevent floating •
phenomena phenomena
the input pin will read a high state • the input pin will read a low state when •
when the button is not pressed the button is not pressed
Internal pull up

The development in the field of semiconductors has led to the •


emergence of integrated circuits, which are integrated circuits
on field of semiconductors has led to the emergence of
integrated circuits, which are integrated circuits on small chips
The development of integrated circuits led to the emergence of
the so-called microcontrollers
conditions

switch
If
If statement
1- if statement
if (/* if condition */)
{
// if body
}
else if (/*else if condition */ )
{
// if body
}
else
{
// else body
}
Nested If
Nested If in C Programming is placing If •
Statement inside another IF Statement.
Nested If in C is helpful if you want to check the •
condition inside a condition.
Syntax of nested if

if ( test condition 1) { //If the test condition 1 is TRUE then •


these it will check for test condition 2
if ( test condition 2) { //If the test condition 2 is TRUE, these •
statements execute Test condition 2 True statements; }
else { //If the c test condition 2 is FALSE, then these •
statements execute Test condition 2 False statements; }}
else { //If the test condition 1 is FALSE then these •
statements will be executed Test condition 1 False
statements; }
switch case
The switch statement allows us to execute one code •
block among many alternatives.
You can do the same thing with the if...else..if ladder.
However, the syntax of the switch statement is much
easier to read and write.
Syntax of switch case

switch (expression) ​{ •
case constant1: // statements •
break;
case constant2: // statements break; •
default: // default statements •
} •
Command in arduino
To write value to a digital pin use the command
digitalWrite(pin,HIGH ) if you want to write 5v •
to the pin
OR digitalWrite(pin,LOW ) if you want to write
zero v to the pin
To read a value from a digitalpin use the
command digitalRead(pin).
The second project is to turn on led with a
push button

use pull down resistor for button connection .


code
#define LED_PIN 8
#define BUTTON_PIN 5
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) {
digitalWrite(LED_PIN, HIGH);
}
else {
digitalWrite(LED_PIN, LOW);}
}
delay(200);}
Proteus design
by using internal pull up
code
#define LED_PIN 8
#define BUTTON_PIN 5
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
digitalWrite(LED_PIN, HIGH);
}
else {
digitalWrite(LED_PIN, LOW);}
}
Proteus design
Switch bouncing
Switch bounce is a common problem associated with mechanical 
switches and relays.
It relies on the fact that bouncing takes a maximum period of 20- 
30 ms.
It’s similar to case of dropping a bouncing ball or 
basketball. The ball keeps on bouncing till it comes to rest
switch debouncing

The solution of the switch bouncing is •


switch debouncing, which can be hardware
or software.
We will implement software switch •
debouncing by using delay
code
#define LED_PIN 8
#define BUTTON_PIN 5
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) {
delay(20);
if (digitalRead(BUTTON_PIN) == HIGH){
digitalWrite(LED_PIN, HIGH);
}
else {
digitalWrite(LED_PIN, LOW);}}}
The Analog World
Microcontrollers are capable of detecting binary signals: is the button pressed or not? •
These are digital signals.
When a microcontroller is powered from five volts, it understands zero volts (0V) as a •
binary 0 and a five volts (5V) as a binary 1.
The world however is not so simple What if the signal is 2.72V? Is that a zero or a one? •
We often need to measure signals that vary; these are called analog signals.
A 5V analog sensor may output 0.01V or 4.99V or anything in between. •
Luckily, nearly all microcontrollers have a device built into them that allows us to •
convert these voltages into values that we can use in a program to make a decision
ADC

An Analog to Digital Converter (ADC) is a very useful •


feature that converts an analog voltage on a pin to a digital
number. By converting from the analog world to the digital
world, we can begin to use electronics to interface to the
analog world around us.
Arduino ADC
Not every pin on a microcontroller has the ability to do •
analog to digital conversions.
On the Arduino board, these pins have an ‘A’ in front of their •
label (A0 through A5) to indicate these pins can read analog
voltages.
The ADC on the Arduino is a 10-bit ADC meaning it has the •
ability to detect 1024 (2^10) discrete analog levels

You might also like