[go: up one dir, main page]

0% found this document useful (0 votes)
54 views25 pages

Arduino Example

The document contains 13 projects related to Arduino programming. Project 3 involves PWM to gradually increase and decrease the brightness of an LED. Project 4 creates a basic traffic light using 3 LEDs. Project 5 creates a chasing light effect using multiple LEDs. The projects involve concepts like analog/digital input/output, buttons, buzzers, temperature sensors, and more. Each project contains a brief description and the code to implement the concept.

Uploaded by

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

Arduino Example

The document contains 13 projects related to Arduino programming. Project 3 involves PWM to gradually increase and decrease the brightness of an LED. Project 4 creates a basic traffic light using 3 LEDs. Project 5 creates a chasing light effect using multiple LEDs. The projects involve concepts like analog/digital input/output, buttons, buzzers, temperature sensors, and more. Each project contains a brief description and the code to implement the concept.

Uploaded by

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

Project - 3

/*
keyestudio super learning kit
Project 3
pwm
http//www.keyestudio.com
*/
int ledPin = 10;
void setup() {
pinMode(ledPin,OUTPUT);
}
void loop(){
for (int value = 0 ; value < 255; value=value+1){
analogWrite(ledPin, value);
delay(5);
}
for (int value = 255; value >0; value=value-1){
analogWrite(ledPin, value);
delay(5);
}
}

Project - 4

/*
keyestudio super learning kit
Project 4
traffic light
http//www.keyestudio.com
*/
int redled =10; // initialize digital pin 10.
int yellowled =7; // initialize digital pin 7.
int blueled =4; // initialize digital pin 4.
void setup()
{
pinMode(redled, OUTPUT);// set the pin with red LED as “output”
pinMode(yellowled, OUTPUT); // set the pin with yellow LED as “output”
pinMode(blueled, OUTPUT); // set the pin with blue LED as “output”
}
void loop()
{
digitalWrite(blueled, HIGH);//// turn on blue LED
delay(5000);// wait 5 seconds
digitalWrite(blueled, LOW); // turn off blue LED
for(int i=0;i<3;i++)// blinks for 3 times
{
delay(500);// wait 0.5 second
digitalWrite(yellowled, HIGH);// turn on yellow LED
delay(500);// wait 0.5 second
digitalWrite(yellowled, LOW);// turn off yellow LED
}
delay(500);// wait 0.5 second
digitalWrite(redled, HIGH);// turn on red LED
delay(5000);// wait 5 second
digitalWrite(redled, LOW);// turn off red LED
}

Project – 5

/*
keyestudio super learning kit
Project 5
LED Chasing Effect
http//www.keyestudio.com
*/
int BASE = 2 ; // the I/O pin for the first LED
int NUM = 6; // number of LEDs
void setup()
{
for (int i = BASE; i < BASE + NUM; i ++)
{
pinMode(i, OUTPUT); // set I/O pins as output
}
}
void loop()
{
for (int i = BASE; i < BASE + NUM; i ++)
{
digitalWrite(i, LOW); // set I/O pins as “low”, turn off LEDs one
by one.
delay(200); // delay
}
for (int i = BASE; i < BASE + NUM; i ++)
{
digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one
by one
delay(200); // delay
}
}

Project – 6

/*
keyestudio super learning kit
Project 6
Button
http//www.keyestudio.com
*/
int ledpin=11;// initialize pin 11
int inpin=7;// initialize pin 7
int val;// define val
void setup()
{
pinMode(ledpin,OUTPUT);// set LED pin as “output”
pinMode(inpin,INPUT);// set button pin as “input”
}
void loop()
{
val=digitalRead(inpin);// read the level value of pin 7 and assign if to
val
if(val==LOW)// check if the button is pressed, if yes, turn on the LED
{ digitalWrite(ledpin,LOW);}
else
{ digitalWrite(ledpin,HIGH);}
}
Project – 7

/*
keyestudio super learning kit
Project 7
Active Buzzer
http//www.keyestudio.com
*/
int buzzer=8;// initialize digital IO pin that controls the buzzer
void setup()
{
pinMode(buzzer,OUTPUT);// set pin mode as “output”
}
void loop()
{
digitalWrite(buzzer, HIGH); // produce sound
}

Project – 8

/*
keyestudio super learning kit
Project 8
Passive Buzzer
http//www.keyestudio.com
*/
int buzzer=8;// select digital IO pin for the buzzer
void setup()
{
pinMode(buzzer,OUTPUT);// set digital IO pin pattern, OUTPUT to be output
}
void loop()
{ unsigned int i,j;//define variable
while(1)
{ for(i=0;i<80;i++)// output a frequency sound
{ digitalWrite(buzzer,HIGH);// sound
delay(1);//delay1ms
digitalWrite(buzzer,LOW);//not sound
delay(1);//ms delay
}
for(j=0;j<100;j++)// output a frequency sound
{ digitalWrite(buzzer,HIGH);// sound
delay(2);//2ms delay
digitalWrite(buzzer,LOW);//not sound
delay(2);//2ms delay
}}}

Project – 9

int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin =9;// select the pin for the green LED
int val;
void setup() {
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
for(val=255; val>0; val--)
{
analogWrite(11, val);
analogWrite(10, 255-val);
analogWrite(9, 128-val);
delay(1);
}
for(val=0; val<255; val++)
{
analogWrite(11, val);
analogWrite(10, 255-val);
analogWrite(9, 128-val);
delay(1);
}
Serial.println(val, DEC);
}
Project 10

/*
keyestudio super learning kit
Project 10
Photo Resistor
http//www.keyestudio.com
*/
int potpin=0;// initialize analog pin 0, connected with photovaristor
int ledpin=11;// initialize digital pin 11,
int val=0;// initialize variable va
void setup()
{
pinMode(ledpin,OUTPUT);// set digital pin 11 as “output”
Serial.begin(9600);// set baud rate at “9600”
}
void loop()
{
val=analogRead(potpin);// read the value of the sensor and assign it to
val
Serial.println(val);// display the value of val
analogWrite(ledpin,val/4);// set up brightness(maximum value 255)
delay(10);// wait for 0.01
}
Project 11

int flame=0;// select analog pin 0 for the sensor


int Beep=9;// select digital pin 9 for the buzzer
int val=0;// initialize variable
void setup()
{
pinMode(Beep,OUTPUT);// set LED pin as “output”
pinMode(flame,INPUT);// set buzzer pin as “input”
Serial.begin(9600);// set baud rate at “9600”
}
void loop()
{
val=analogRead(flame);// read the analog value of the sensor
Serial.println(val);// output and display the analog value
if(val>=600)// when the analog value is larger than 600, the buzzer will
buzz
{
digitalWrite(Beep,HIGH);
}else
{
digitalWrite(Beep,LOW);
}
delay(500);
}
Project 12

int potPin = 0; // initialize analog pin 0 for LM35 temperature sensor


void setup()
{
Serial.begin(9600);// set baud rate at”9600”
}
void loop()
{
int val;// define variable
int dat;// define variable
val=analogRead(0);// read the analog value of the sensor and assign it to
val
dat=(125*val)>>8;// temperature calculation formula(divide by 2power8)
Serial.print("Tep");// output and display characters beginning with Tep
Serial.print(dat);// output and display value of dat
Serial.println("C");// display “C” characters
delay(500);// wait for 0.5 second
}
/*
Project 13

/*
keyestudio super learning kit
Project 13
Tilt Switch
http//www.keyestudio.com
*/
void setup()
{
pinMode(8,OUTPUT);// set digital pin 8 as “output”
}
void loop()
{
int i;// define variable i
while(1)
{
i=analogRead(5);// read the voltage value of analog pin 5
if(i>512)// if larger that 512(2.5V)
{
digitalWrite(8,LOW);// turn on LED
}
else// otherwise
{
digitalWrite(8,HIGH);// turn off LED
} } }
Push on/off

#include <stdio.h>
#include <stdlib.h>

const int button = 8; // GPIO 8 for the button


const int led =7; // GPIO 7 for the LED
int ledflag=0; // LED status flag

void setup() {
pinMode(button,INPUT); // define button as an input
pinMode(led,OUTPUT); // define LED as an output
digitalWrite(led,LOW); // turn output off just in case
}

void loop() {
if (digitalRead(button)==HIGH){ // if button is pressed
if (ledflag==0) { // and the status flag is LOW
ledflag=1; // make status flag HIGH
digitalWrite(led,HIGH); // and turn on the LED
} //
else { // otherwise...
ledflag=0; // make status flag LOW
digitalWrite(led,LOW); // and turn off the LED
}
delay(1000); // wait a sec for the
} } // hardware to stabilize
/*
AnalogReadSerial

Reads an analog input on pin 0, prints the result to the Serial


Monitor.
Graphical representation is available using Serial Plotter (Tools >
Serial Plotter menu).
Attach the center pin of a potentiometer to pin A0, and the outside
pins to +5V and ground.

This example code is in the public domain.

https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogReadSerial
*/

// the setup routine runs once when you press reset:


void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
/*
Soil Moisture Sensor
modified on 21 Feb 2019
by Saeed Hosseini @ Electropeak
https://electropeak.com/learn/
*/
#define SensorPin A0
float sensorValue = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i <= 100; i++)
{
sensorValue = sensorValue + analogRead(SensorPin);
delay(1);
}
sensorValue = sensorValue/100.0;
Serial.println(sensorValue);
delay(30);

Code for PIR

int ledPin = 13; // LED


int pirPin = 2; // PIR Out pin
int pirStat = 0; // PIR status
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
Serial.begin(9600);
}
void loop(){
pirStat = digitalRead(pirPin);
if (pirStat == HIGH) { // if motion detected
digitalWrite(ledPin, HIGH); // turn LED ON
Serial.println("Hey I got you!!!");
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF if we have no motion
}
}
Code Explanation
First we have to include the "DHT.h" Library.
#include "DHT.h"

Then define the digital pin in which the DHT11 is connected to.
#define DHTPIN 2 // Digital pin connected to the DHT sensor
Now define the type of DHT Sensor. Since we're using a DHT11 sensor, we can
write like this.
#define DHTTYPE DHT11 // DHT 11

If you have a DHT22, write this.


#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

Then define the DHT parameter to Initialize DHT sensor.


DHT dht(DHTPIN, DHTTYPE);
Note that older versions of this library took an optional third parameter to
tweak the timings for faster processors. This parameter is no longer needed as
the current DHT reading algorithm adjusts itself to work on faster procs.
Inside the void setup function, Initialize the Serial Communication and the
DHT Sensor.
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));

dht.begin();
}

Now, inside the void loop function, let's measure the readings.
Reading temperature or humidity takes about 250 milliseconds! Sensor
readings may also be up to 2 seconds 'old' (its a very slow sensor).
float h = dht.readHumidity(); // read humidity

Read temperature as Celsius (the default)


float t = dht.readTemperature(); // read temperature
Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}

Compute heat index in Fahrenheit (the default)


float hif = dht.computeHeatIndex(f, h);

Compute heat index in Celsius (isFahreheit = false)


float hic = dht.computeHeatIndex(t, h, false);

Print the measured readings on the Serial Monitor.


Serial.print(F(" Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));

while(1)
It is an infinite loop which will run till a break statement is issued explicitly. Interestingly not
while(1) but any integer which is non-zero will give a similar effect as while(1). Therefore,
while(1), while(2) or while(-255), all will give infinite loop only.
while(1) or while(any non-zero integer)
{
// loop runs infinitely
}
A simple usage of while(1) can be in the Client-Server program. In the program, the server
runs in an infinite while loop to receive the packets sent from the clients.
But practically, it is not advisable to use while(1) in real-world because it increases the CPU
usage and also blocks the code i.e one cannot come out from the while(1) until the program is
closed manually. while(1) can be used at a place where condition needs to be true always.

// C program to illustrate while(1)

#include <stdio.h>

int main()

{
int i = 0;

while (1) {

printf("%d\n", ++i);

if (i == 5)

break; // Used to come

// out of loop

return 0;

Output
1
2
3
4
5
while(0)
It is opposite of while(1). It means condition will always be false and thus code in while will
never get executed.
while(0)
{
// loop does not run
}
// C program to illustrate while(0)

#include<stdio.h>

int main()

int i = 0, flag=0;

while ( 0 )

// This line will never get executed

printf( "%d\n", ++i );

flag++;

if (i == 5)

break;

if (flag==0)

printf ("Didn't execute the loop!");

return 0;

Output
Didn't execute the loop!
I have been trying to exit from the while(1) loop by getting a value from the sensor and then run
the code outside while(1).But the problem is that it does not exit. How may I fix this problem?

See Code Below.......

void setup() {
pinMode(2,INPUT);
Serial.begin(9600);
}

void loop() {
int sensor;
while(1)
{
sensor = digitalRead(2);
if(sensor == 0)
{
Serial.println("inside while loop");
}
break;
}
Serial.println("Break......");
}
correction
void setup() {
pinMode(2,INPUT);
Serial.begin(9600);
}

void loop() {
bool cond = true;

int sensor;
while(cond)
{
sensor = digitalRead(2);
if(sensor == 0)
{
Serial.println("inside while loop");
}
cond = false;
}

// like this you can used while inside the void loop()

}
void setup() {
pinMode(2,INPUT);
Serial.begin(9600);
int sensor;
}
void loop() {
while(1)
{
sensor = digitalRead(2);
if(sensor == 1)
break;
else
{Serial.println("inside while loop"); delay(500);}
}
Serial.println("Break......");
Delay(500);
}
int sensorPin =A0 ; // define analog port A0
int value = 0; //set value to 0
void setup()
{
Serial.begin(9600); //set the baud rate to 9600
}
void loop()
{
value = analogRead(sensorPin); //set the value as the value read from A0
Serial.println(value, DEC); //print the value and line wrap
delay(200); //delay 0.2S
}

Microphone sensor

1. Digital Write
2. Analog Write(project-3)
3. Digital Read (button-proj6 & toggle switch)
4. Analog Read, Serial Read and Traffic(proj5)
5. LED Chasing(proj5), Active Bazar(proj7) & Passive Bazar (Proj8)

You might also like