microcontrollers ppt

65
What is an LED? • Light-emitting diode • Semiconductor • Has polarity

Upload: sree-ram

Post on 15-Jul-2016

246 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: microcontrollers ppt

What is an LED?

• Light-emitting diode• Semiconductor• Has polarity

Page 2: microcontrollers ppt

LED: How It Works

• When current flows across a diode

• Negative electrons move one way and positive holes move the other way

Page 3: microcontrollers ppt

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

Page 4: microcontrollers ppt

Inside a Light Emitting Diode

1. Transparent Plastic Case

2. Terminal Pins3. Diode

                                                                  

                                                                                                                                                                     

Page 5: microcontrollers ppt

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

Page 6: microcontrollers ppt

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.

Page 7: microcontrollers ppt

Sensors FunctionPush Button On/Off Switch PIR Human Detector

SENSORS• Digital Sensors:

Sensors VariablesMic Sound VolumePhotoresistor Light LevelPotentiometer Voltage DividerTemp Sensor TemperatureFlex Sensor bendAccelerometer tilt/acceleration

• Analog Sensors:

Page 8: microcontrollers ppt

Basic SensorsPush Button Digital Input Switch - Closes

or opens circuitPolarized, 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

Name Image Type Function Notes

Page 9: microcontrollers ppt

Flex Sensor Application

Page 10: microcontrollers ppt

Relay

Page 11: microcontrollers ppt

Trim pot (Potentiometer)Variable Resistor

wiper

fixedend

fixedend

Page 12: microcontrollers ppt

3 Pin Potentiometer = var. resistor a.k.a. Voltage Divider Circuit

1.0 V 1.0 V

wiper

Fixed ends

Page 13: microcontrollers ppt

Servo Motor

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

Page 14: microcontrollers ppt

Programming Concepts: Variable Types

• Variable Types:

8 bits 16 bits 32 bits

byte char

intunsigned int

longunsigned longfloat

Page 15: microcontrollers ppt

Arduino Integrated Development Environment (IDE)

Two required functions / methods / routines:

void setup(){

// runs once}

void loop(){

// repeats}error & status messages

Page 16: microcontrollers ppt

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.

Page 17: microcontrollers ppt

Settings: Tools Board

•Next, double-check that the proper board is selected under the ToolsBoard menu.

Page 18: microcontrollers ppt

ADC (A/D Converter)

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

1023• 0 V 0• 5 V 1023

Example:• int sensorValue = analogRead(A0);

Page 19: microcontrollers ppt

digitalWrite()

analogWrite()

digitalRead()

if() statements / Boolean

analogRead()

Serial communication

BIG

6 CO

NCE

PTS

Page 20: microcontrollers ppt

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

Page 21: microcontrollers ppt

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

Page 22: microcontrollers ppt

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

Page 23: microcontrollers ppt

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

Page 24: microcontrollers ppt

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

• // it’s good to put a description at the top and before anything ‘tricky’

• /* this is for multi-line comments• Like this… • And this….• */

Page 25: microcontrollers ppt

comments

Page 26: microcontrollers ppt

Let’s 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

Page 27: microcontrollers ppt

Project #1: LED Blink

Page 28: microcontrollers ppt

Project # 1:Codeint 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}

Page 29: microcontrollers ppt

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???

Page 30: microcontrollers ppt

Project #2: LED Fading

Page 31: microcontrollers ppt

Project # 2 : Codeint ledPin = 9;    // LED connected to digital pin 9void setup()  { } 

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);                              } }

Page 32: microcontrollers ppt

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.

Page 33: microcontrollers ppt

Project #3: Push Button

Digital Pin 2

Page 34: microcontrollers ppt

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

Page 35: microcontrollers ppt

Project # 3:Codeconst int buttonPin = 2;     // the number of the pushbutton pinint buttonState = 0;         // variable for pushbutton statusint led = 13; // indication ledvoid 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);    }}

Page 36: microcontrollers ppt

Digital Sensor - Push ButtonAdd an Indicator LED to Pin 13

This is combination of 1 and 3 project

Page 37: microcontrollers ppt

const int buttonPin = 2;     // the number of the pushbutton pinconst int ledPin =  13;      // the number of the LED pinint buttonState = 0;         // variable for reading the pushbutton statusvoid 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);   }}

Page 38: microcontrollers ppt

Project #4: PIR Sensor

Page 39: microcontrollers ppt

const int buttonPin = 2;     // the number of the pushbutton pinint buttonState = 0;         // variable for reading the pushbutton statusvoid 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: Code

Page 40: microcontrollers ppt

Challenge 4a – Automatic Electrical Appliance On/Off Control Algorithm.

Challenge 4b – Home Security System.

Project # 4: Challenges

Page 41: microcontrollers ppt

Project #5: Servo Motor

Page 42: microcontrollers ppt

#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

Page 43: microcontrollers ppt

Project #5: Code Continued..for(pos = 180; pos>=0; pos-=1) // from 180 degrees to 0 { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }

Page 44: microcontrollers ppt

Project #6: Proximity Sensor (HC-SR04)

Page 45: microcontrollers ppt

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

Page 46: microcontrollers ppt

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);}

Page 47: microcontrollers ppt

Challenge 6a – Automatic Tollgate System

Project # 6: Challenges

Challenge 6b – Automatic Human Follower

Page 48: microcontrollers ppt

Project #7: Potentiometer (POT)

Page 49: microcontrollers ppt

Project #7: Codeint sensorPin = A0;    // select the input pin for the potentiometerint sensorValue = 0;  // variable to store the value coming from the sensor

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);                  }

Page 50: microcontrollers ppt

Project #8: LDR

Page 51: microcontrollers ppt

Project #8 : Code

int sensorPin = A0;    // select the input pin for the potentiometerint sensorValue = 0;  // variable to store the value coming from the sensor

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);                  }

Page 52: microcontrollers ppt

Challenge 9a – Automatic Light On/Off Control Algorithm.

Challenge 9b – Automatic Solar Tracking System.

Project # 9: Challenges

Page 53: microcontrollers ppt

Project #10: Temperature Sensor (LM35)

Page 54: microcontrollers ppt

Project #10 : Code

int sensorPin = A0;    // select the input pin for the potentiometerint sensorValue = 0;  // variable to store the value coming from the sensor

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);                  }

Page 55: microcontrollers ppt

Challenge 10a – Automatic Fan On/Off Control Algorithm.

Project # 10: Challenges

Page 56: microcontrollers ppt

Project #11: Accelerometer (ADXL335)

Page 57: microcontrollers ppt

const int xpin = A3; // x-axisconst int ypin = A2; // y-axisconst int zpin = A1; // 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 : Code

Page 58: microcontrollers ppt

Challenge 11a – Control of Appliance using Hand Gestures.

Project # 11: Challenges

Page 59: microcontrollers ppt

Project # 12: BLUETOOTH

Page 60: microcontrollers ppt

Project # 12: Codechar val;       // variable to receive data from the serial portint ledpin = 2;  // LED connected to pin 2

void setup(){  pinMode(ledpin = 13, OUTPUT);  // pin 13 (on-board LED) as OUTPUT Serial.begin(115200); // start serial communication at 115200bps } void loop() {  if( Serial.available() )       // if data is available to read {    val = Serial.read();         // read it and store it in 'val'  if( val == '0' )               // if '0' was received led 13 is switched off {   digitalWrite(ledpin, LOW);    // turn led off delay(1000);                  // waits for a second    }

Page 61: microcontrollers ppt

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: Code Continued..

Challenge 12 – Home Automation

Project # 12: Challenges

Page 62: microcontrollers ppt

Project # 13: DTMF

Page 63: microcontrollers ppt

Project # 13: Codeint d0pin=2;int d1pin=3;int d2pin=4;int d3pin=5;int relaypin1=7; // initializationsint relaypin2=8;int d0state =0;int d1state =0;int d2state =0;int d3state =0;

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

Page 64: microcontrollers ppt

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); } }

Project # 13: Code Continued..

Page 65: microcontrollers ppt

THANK YOU