microcontrollers ppt

Post on 15-Jul-2016

246 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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 Pins3. 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 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:

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

Flex Sensor Application

Relay

Trim pot (Potentiometer)Variable Resistor

wiper

fixedend

fixedend

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

1.0 V 1.0 V

wiper

Fixed ends

Servo Motor

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

Programming Concepts: Variable Types

• Variable Types:

8 bits 16 bits 32 bits

byte char

intunsigned int

longunsigned longfloat

Arduino Integrated Development Environment (IDE)

Two required functions / methods / routines:

void setup(){

// runs once}

void loop(){

// repeats}error & status messages

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• 0 V 0• 5 V 1023

Example:• int sensorValue = analogRead(A0);

digitalWrite()

analogWrite()

digitalRead()

if() statements / Boolean

analogRead()

Serial communication

BIG

6 CO

NCE

PTS

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

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

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

comments

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

Project #1: LED Blink

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}

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

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-port-communication/

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

Digital Sensor - Push ButtonAdd an Indicator LED to Pin 13

This is combination of 1 and 3 project

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

Project #4: PIR Sensor

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

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

Challenge 4b – Home Security System.

Project # 4: Challenges

Project #5: Servo Motor

#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

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

Project #6: Proximity Sensor (HC-SR04)

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

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

Challenge 6a – Automatic Tollgate System

Project # 6: Challenges

Challenge 6b – Automatic Human Follower

Project #7: Potentiometer (POT)

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

Project #8: LDR

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

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

Challenge 9b – Automatic Solar Tracking System.

Project # 9: Challenges

Project #10: Temperature Sensor (LM35)

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

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

Project # 10: Challenges

Project #11: Accelerometer (ADXL335)

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

Challenge 11a – Control of Appliance using Hand Gestures.

Project # 11: Challenges

Project # 12: BLUETOOTH

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    }

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

Project # 13: DTMF

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

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

THANK YOU

top related