Arduino Workshop
Arduino Workshop
Arduino Basics
What is a Microcontroller?
• A miniature programmable computer
• Contains memory
• RAM
• Flash
• Peripherals
• Serial communications
• Pulse Width Modulation
• Analog Digital Converter
Arduino
Reset Button(10):
Pushing it will temporarily
connect the reset pin to ground
and restart any code that is
loaded on the Arduino. This can
be very useful if your code
doesn’t repeat, but you want to
test it multiple times.
Arduino Hardware
• void loop()
• Runs forever
• Code that does actual work goes here
Analog vs Digital
Essential Functions
• pinMode()
• digitalWrite()
• digitalRead()
• analogWrite()
• analogRead()
• delay()
• Serial.begin()
• Serial.print()
pinMode() Function
The pinMode() function is used to configure a specific pin to behave either as an input or an output.
It is possible to enable the internal pull-up resistors with the mode INPUT_PULLUP. Additionally, the
INPUT mode explicitly disables the internal pull-ups.
Syntax
void setup()
{
pinMode(pin, mode);
}
• pin − the number of the pin whose mode you wish to set
• mode − INPUT, OUTPUT, or INPUT_PULLUP.
digitalWrite() Function
The digitalWrite() function is used to write a HIGH or a LOW value to a digital pin. If the pin has
been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value:
5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
Syntax
void loop()
{
digitalWrite(pin, value);
}
• pin − the number of the pin where you wish to set the value
• value −HIGH or LOW
digitalRead() Function
The digitalRead() function is used to write a HIGH or a LOW value to a digital pin. If the pin has been
configured as an INPUT with pinMode(), its voltage will be read to the corresponding value: 1 for 5V,
0 for 0V.
Syntax
void loop()
{
digitalRead(pin);
}
• pin − the number of the pin where you wish to read the value
analogRead( ) function
In the lower-right part of the Arduino board, you will see six pins marked
“Analog In”. These special pins not only tell whether there is a voltage applied
to them, but also its value. By using the analogRead() function, we can read
the voltage applied to one of the pins.
This function returns a number between 0 and 1023, which represents
voltages between 0 and 5 volts. For example, if there is a voltage of 2.5 V
applied to pin number 0, analogRead(0) returns 512.
Syntax
void loop()
{
analogRead(pin);
}
• pin − the number of the pin whose value you wish to read
analogWrite() Function
The analogWrite() function writes an analog value (PWM wave) to a pin.
After a call of the analogWrite() function, the pin will generate a steady
square wave of the specified duty cycle until the next call to analogWrite() or
a call to digitalRead() or digitalWrite() on the same pin. The frequency of the
PWM signal on most pins is approximately 490 Hz. On the Uno and similar
boards, pins 5 and 6 have a frequency of approximately 980 Hz.
Syntax
void loop()
{
analogWrite(pin, value);
}
• pin − the number of the pin where you wish to set the value
• value −HIGH or LOW
Serial.begin()
Sets the data rate in bits per second (baud) for serial data transmission. For
communicating with the computer, use one of these rates: 300, 600, 1200,
2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200
Syntax:
void setup()
{
Serial.begin(9600);
}
Serial.print()
Used to print data onto the serial monitor.
Serial.print will keep cursor on same line after printing
Serial.println will move cursor to the next line after printing
It can work only if Serial.begin() function is present
Syntax:
void loop()
{
Serial.println (“Hello World”);
}
Breadboard
Used to make connections simpler.
Likely to be used in
projects
External Led Connection
Infrared Sensor
Used to Detect Obstacles.
Connected to a digital
pin of the arduino
Infrared Sensor
Code
const int IRSensor=7;// IR Sensor connected to pin 7
void setup()
{
Serial.begin(9600);
pinMode(IRSensor,INPUT); //Setting Mode of pin 7 as INPUT
}
void loop()
{
int a=digitalRead(IRSensor);//Reading value of the IR Sensor
Serial.println(a); //Printing value in the Serial Monitor
delay(500);
}
Light Dependant Resistor (LDR)
Measures Light Intensity.
It has four legs out of which two legs are internally connected.
CODE
int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
}
else {
digitalWrite(ledPin, HIGH); // turn LED ON
} }
Buzzer
Code
const int buzzer = 9; //buzzer to arduino pin 9
void setup(){
void loop(){
}
Servo Motor
A servo motor is an electrical device
which can push or rotate an object
with great precision. If you want to
rotate and object at some specific
angles or distance, then you use
servo motor. It is just made up of
simple motor which run through
servo mechanism
Code
#include <Servo.h>
Servo servo;
void setup() {
// put your setup code here, to run once:
servo.attach(8);
servo.write(0);
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
servo.write(90);
delay(1000);
servo.write(0);
delay(1000);
}
C language Basics
Lets goooo…….
Structure of a C program
C Control Statements
If Statement :-
The if statement is used to check some given condition and perform some operations depending upon the correctness of that
condition. It is mostly used in the scenario where we need to perform the different operations for the different conditions. The syntax of
the if statement is given below.
if(expression){
//code to be executed
}
for example:-
#include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}
Output
Enter a number:4
4 is even number
If-else Statement:-
The if-else statement is used to perform two operations for a single condition. The if-else statement is
an extension to the if statement using which, we can perform two different operations, i.e., one is for
the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must
notice that if and else block cannot be executed simiultaneously. Using if-else statement is always
preferable since it always invokes an otherwise case with every if condition. The syntax of the if-else
statement is given below.
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
Output
printf("Sorry ... you can't vote");
Enter your age?18
} You are eligible to vote...
Enter your age?13
} Sorry ... you can't vote
If else-if ladder Statement:-
The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where
there are multiple cases to be performed for different conditions.
C Switch Statement:-
The switch statement in C is an alternate to if-else-if ladder statement which allows us to
execute multiple operations for the different possibles values of a single variable called switch
variable. Here, We can define various statements in the multiple cases for the different values of
a single variable.
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch(x>y && x+y>0)
{
case 1:
printf("hi");
break;
case 0: Output
printf("bye"); hi
break;
default:
printf(" Hello bye ");
}
}
C Loops
vThe looping can be defined as repeating the same process multiple times until a specific condition
satisfies.
Advantage of loops in C
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
3) Using loops, we can traverse over the elements of data structures (array or linked lists).
Types of C Loops
There are three types of loops in C language that is given below:
1.do while
2.while
3. for
qdo-while loop in C
The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is
used when it is necessary to execute the loop at least once (mostly menu driven programs).
do{
//code to be executed
}while(condition);
#include<stdio.h>
int main(){
int i=1;
do{
printf("%d \n",i);
i++;
}while(i<=10);
return 0; Output
1
} 2
3
4
5
6
7
8
9
10
qwhile loop in C
The while loop in c is to be used in the scenario where we don't know the number of iterations in
advance. The block of statements is executed in the while loop until the condition specified in the
while loop is satisfied. It is also called a pre-tested loop.
The syntax of while loop in c language is given below:
while(condition){
//code to be executed
}
#include<stdio.h> Output
1
int main(){ 2
3
int i=1; 4
while(i<=10){ 5
6
printf("%d \n",i); 7
8
i++; 9
10
}
return 0;
}
q for loop
The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for
loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance.
The syntax of for loop in c language is given below:
for(initialization;condition;incr/decr){
//code to be executed
}
#include<stdio.h>
int main(){
int i=0;
for(i=1;i<=10;i++){
printf("%d \n",i);
}
return 0;
}
#include<stdio.h>
int main(){
int i=0;
Output
for(i=1;i<=10;i++){ 1
printf("%d \n",i);
2
3
}
4
5
return 0; 6
7
} 8
9
10
TASKS
Application on components discussed
TASK 1
Using Four LEDS make them blink alternately, first with a gap of 4 seconds, then
with a gap of 3 seconds,then with a gap of 2 seconds, then with a gap of 1 second
and finally light all up together and repeat the process in the least possible number
of lines.
TASK 2
Using 4 LEDs write an arduino code to display 0000 upto 1111 in the least
possible number of lines.
TASK 3
Room should turn on only if it is dark inside and if someone enters the room.
TASK 5
Sheldon is working with an undisclosed nation to encrypt nuclear war
codes.
Unfortunately cyber terrorists have hacked into his personal systems and
the only option he has is to build cryptographic systems on his Arduino
Uno
The Red LED indicates the closed gate. The Green LED indicates
the opened gate.
The Gate should be opened to allow passage of the vehicle only after
amount is paid and validated. If the vehicle is detected is a four
wheeler then Rs 50 should be taken from the driver and the
remaining should be returned, if the vehicles detected is a two
wheeler, then Rs 30 should be taken from the rider and the remaining
should be returned.
The user should enter what type of vehicle it is and amount being
paid using Serial Monitor.
All transactions are done via Serial Monitor.