You are on page 1of 65

What is an LED?

Light-emitting diode
Semiconductor
Has polarity

LED: How It Works


When current
flows across a
diode

Negative electrons move one


way and positive holes move
the other way

LED: How It Works


The wholes exist
at a lower energy
level than the free
electrons

Therefore when a free


electrons falls it losses energy

Inside a Light Emitting Diode

1. Transparent
Plastic Case
2. Terminal Pins
3. Diode

Main LED materials


The main semiconductor materials used to manufacture
LEDs are:
Indium gallium nitride (InGaN):blue, green and
ultraviolet high-brightness LEDs
Aluminum gallium indium phosphide
(AlGaInP):yellow, orange and red high-brightness LEDs
Aluminum gallium arsenide (AlGaAs):red and infrared
LEDs
Gallium phosphide (GaP):yellow and green LEDs

What is a Sensor ?
Def. 1. (Oxford dictionary)
A device giving a signal for the detection or
measurement of a physical property to which it
responds.
Def. 2.
A sensor is a device that receives a signal or
stimulus and response with an electrical signal.

SENSORS
Digital Sensors:
Sensors
Push Button
PIR

Function
On/Off Switch
Human Detector

Analog Sensors:

Sensors
Mic
Photoresistor
Potentiometer
Temp Sensor
Flex Sensor
Accelerometer

Variables
Sound Volume
Light Level
Voltage Divider
Temperature
bend
tilt/acceleration

Basic Sensors
Name

Image

Type

Function

Notes

Push Button

Digital Input

Switch - Closes
or opens circuit

Polarized, needs
resistor

Trim
potentiometer

Analog Input

Variable resistor

Also called a Trim


pot.

Photo resistor

Analog Input

Light Dependent
Resistor (LDR)

Resistance varies
with light.

Relay

Digital Output

Switch driven by
a small signal

Used to control
larger voltages

Temp Sensor

Analog Input

Temp Dependent
Resistor

Flex Sensor

Analog Input

Variable resistor

Flex Sensor Application

Relay

Trim pot (Potentiometer)


Variable Resistor

fixed
end
wiper
fixed
end

3 Pin Potentiometer = var. resistor


a.k.a. Voltage Divider Circuit

wiper

Fixed ends

1.0 V

1.0 V

Servo Motor
Servo Connector:
Black Vss
Red Vdd or Vin
White Signal

Programming Concepts: Variable


Types
Variable Types:

8 bits
byte
char

16 bits
int
unsigned int

32 bits
long
unsigned
long
float

Arduino
Integrated Development Environment (IDE)
Two required functions
/ methods / routines:
void setup()
{
// runs once
}

error & status messages

void loop()
{
// repeats
}

Settings: Tools Serial


Port
Your computer
communicates to the
Arduino microcontroller
via a serial port
through a USB-Serial
adapter.
Check to make sure that
the drivers are properly
installed.

Settings: Tools Board

Next, double-check that the proper board is selected


under the ToolsBoard menu.

ADC (A/D Converter)


Arduino uses a 10-bit A/D Converter:
this means that you get input values
from 0 to 1023
0V0
5 V 1023

Example:
int sensorValue = analogRead(A0);

BIG 6 CONCEPTS

digitalWrite()
analogWrite()
digitalRead()
if() statements /
Boolean
analogRead()
Serial
communication

Digital Commands
digitalWrite(pin, HIGH/LOW);

ex: digitalWrite(13, HIGH); // set 13


pin to 5Volts

digitalRead(pin);
ex: int val = digitalRead(3);
// NOTE: -> commands are CASE-sensitive

Analog Commands
analogWrite(pwm pin,value );

ex: analogWrite(9, 255); // set 9 pin


to 255

anlogRead(pin);
ex: int val = anlogRead(0);
// NOTE: -> commands are CASE-sensitive

Serial Commands
Serial.begin(baudrate);

ex: Serial.begin(9600);
Serial.read();
ex: Char c = Serial.read();
Serial.print();
ex: Serial.print(arduino); // print
arduino
// NOTE: -> commands are CASE-sensitive

Four more commands to know


pinMode(pin, INPUT/OUTPUT);

ex: pinMode(13, OUTPUT);


map(val, fromLow, fromHigh, toLow, toHigh) ;

ex: val = map(val, 0, 1023, 0, 255);


constrain(val, min, max) ;

ex: val = constrain(val, 0, 180);


delay(time_ms);
ex: delay(2500); // delay of 2.5 sec.
// NOTE: -> commands are CASE-sensitive

Comment Lines
Comments are for you the programmer and your friends
or anyone else human that might read your code.
// this is for single line comments
// its good to put a description at the top and
before anything tricky
/* this is for multi-line comments

Like this

And this.
*/

comments

Lets get to coding


Project #1 Blink
Hello World of Physical Computing

Psuedo-code how should this work?


Turn
LED ON

Wait

Turn
LED
OFF

Wait

Rinse &
Repeat

Project #1: LED Blink

Project # 1:Code
int led = 13;
// the setup routine runs once when you press
reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again
forever:
void loop() {
digitalWrite(led, HIGH);
// turn the LED on
(HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(led, LOW);
// turn the LED off
by making the voltage LOW
delay(1000);
// wait for a second
}

Project #1: Challenges

Challenge 1a blink with a 200 ms


second interval.
Challenge 1b blink to mimic a heartbeat
Challenge 1c find the fastest blink that
the human eye can still detect
1 ms delay? 2 ms delay? 3 ms delay???

Project #2: LED Fading

Project # 2 : Code
int ledPin = 9;
void setup() {
}

// LED connected to digital pin 9

void loop() {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5){
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5){
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}

Project# 2 Challenges
Challenge 2a Change the rate of
the fading in and out. There are at
least two different ways to do this
can you figure them out?
Challenge 2b Use 2 (or more)
LEDs.

Project #3: Push Button

Digital Pin 2

http://opensourcehardwarejunkies.com/tutorial-03-digitalread-and-serial-por

Project # 3:Code
const int buttonPin = 2;
// the number of the pushbutton pin
int buttonState = 0;
// variable for pushbutton status
int led = 13;
// indication led
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
// initialize serial communication
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
Serial.println(button pressed);
}
else {
digitalWrite(led,HIGH);
delay(900);
}
}

Digital Sensor - Push Button


Add an Indicator LED to Pin 13

This is combination
of 1 and 3 project

const int buttonPin = 2;


// the number of the pushbutton pin
const int ledPin = 13;
// the number of the LED pin
int buttonState = 0;
// variable for reading the
pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Project #4: PIR Sensor

Project #4: Code


const int buttonPin = 2;
// the number of the pushbutton pin
int buttonState = 0;
// variable for reading the
pushbutton status
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
// initialize serial communication
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
Serial.println(motion detected);
}
else {
Serial.println(sorry try once);
}
}

Project # 4: Challenges
Challenge 4a Automatic Electrical
Appliance On/Off Control Algorithm.
Challenge 4b Home Security
System.

Project #5: Servo Motor

Project #5: Code


#include <Servo.h> // predefined servo library
Servo myservo;

// create servo object to control a servo

int pos = 0;

// variable to store the servo position

void setup()
{
myservo.attach(9);
}

// attaches the servo on pin 9

void loop()
{
for(pos = 0; pos <= 180; pos += 1) // from 0 degrees to 180
{
// in steps of 1 degree
myservo.write(pos);
// tell servo to go to position in
variable 'pos'
delay(15);
// waits 15ms for the servo
to reach the position
}

Project #5: Code Continued..


for(pos = 180; pos>=0; pos-=1)
{
myservo.write(pos);
position in variable 'pos'
delay(15);
to reach the position
}
}

// from 180 degrees to 0


// tell servo to go to
// waits 15ms for the servo

Project #6: Proximity Sensor


(HC-SR04)

Project #6: Code


const int trigPin = 2;
const int echoPin = 4;
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

Project #6: Code Continued..


// convert the time into a distance
// 73.746 microseconds per inch (i.e. sound travels at 1130
feet per second)
// we divide by 2 to get the distance of the obstacle.
duration = duration / 74 / 2;
Serial.print(inches);
Serial.print("in, ");
// The speed of sound is 340 m/s or 29 microseconds per
centimeter.
// object we take half of the distance travelled.
duration = duration / 29 / 2;
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);

Project # 6: Challenges
Challenge 6a Automatic Tollgate
System

hallenge 6b Automatic Human Follower

Project #7: Potentiometer (POT)

Project #7: Code


int sensorPin = A0;
potentiometer
int sensorValue = 0;
the sensor

// select the input pin for the


// variable to store the value coming from

void setup() {
Serial.begin(9600); // initialize serial communication
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// print the sensor value
Serrial.println{sensorValue);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}

Project #8: LDR

Project #8 : Code
int sensorPin = A0;
potentiometer
int sensorValue = 0;
the sensor

// select the input pin for the


// variable to store the value coming from

void setup() {
Serial.begin(9600); // initialize serial communication
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// print the sensor value
Serrial.println{sensorValue);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}

Project # 9: Challenges
Challenge 9a Automatic Light
On/Off Control Algorithm.
Challenge 9b Automatic Solar
Tracking System.

Project #10: Temperature


Sensor (LM35)

Project #10 : Code


int sensorPin = A0;
potentiometer
int sensorValue = 0;
the sensor

// select the input pin for the


// variable to store the value coming from

void setup() {
Serial.begin(9600); // initialize serial communication
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// temperature conversion formula
float val = ( sensorValue/1024.0)*5000;

float celisius = val/10;


// print the sensor value
Serrial.println(celisius);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}

Project # 10: Challenges


Challenge 10a Automatic Fan
On/Off Control Algorithm.

roject #11: Accelerometer (ADXL

Project # 11 : Code
const int xpin = A3;
const int ypin = A2;
const int zpin = A1;

// x-axis
// y-axis
// z-axis

void setup()
{
Serial.begin(9600); // initialize the serial communication
}
void loop()
{
// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(ypin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
// delay before next reading:
delay(500);

Project # 11: Challenges


Challenge 11a Control of
Appliance using Hand Gestures.

Project # 12: BLUETOOTH

Project # 12: Code


char val;
int ledpin = 2;

// variable to receive data from the serial port


// LED connected to pin 2

void setup()
{
pinMode(ledpin = 13, OUTPUT);
OUTPUT
Serial.begin(115200); // start
115200bps
}
void loop() {
if( Serial.available() )
{
val = Serial.read();
if( val == '0' )
is switched off
{
digitalWrite(ledpin, LOW);
delay(1000);
}

// pin 13 (on-board LED) as


serial communication at

// if data is available to read


// read it and store it in 'val'
// if '0' was received led 13
// turn led off
// waits for a second

Project # 12: Code Continued..


if( val == '1' )
// if '1' was received led 13 on
{
digitalWrite(ledpin = 13, HIGH); // turn led on
delay(1000);
// waits for a second
}
}

Project # 12: Challenges


Challenge 12 Home Automation

Project # 13: DTMF

int
int
int
int
int
int
int
int
int
int

Project # 13: Code

d0pin=2;
d1pin=3;
d2pin=4;
d3pin=5;
relaypin1=7;
relaypin2=8;
d0state =0;
d1state =0;
d2state =0;
d3state =0;

// initializations

void setup(){
pinMode(d0pin,INPUT);
pinMode(d1pin,INPUT);
pinMode(d2pin,INPUT);
// pin declarations
pinMode(d3pin,INPUT);
pinMode(relaypin1,OUTPUT);
pinMode(relaypin2,OUTPUT);
}

Project # 13: Code Continued..


void loop(){
int d0state=digitalRead(d0pin);
int d1state=digitalRead(d1pin);
int d2state=digitalRead(d2pin);
int d3state=digitalRead(d3pin);
if(d0state==HIGH){
digitalWrite(relaypin1,HIGH);
}
else if(d1state==HIGH){
digitalWrite(relaypin1,LOW);
}
else if(d2state==HIGH){
digitalWrite(relaypin2,HIGH);
}
else if(d3state==HIGH){
digitalWrite(relaypin2,LOW);
}
}

U
O
Y
K
N
A
H
T