* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Introduction to Sensor Technology
Survey
Document related concepts
Current source wikipedia , lookup
Voltage optimisation wikipedia , lookup
Flip-flop (electronics) wikipedia , lookup
Control system wikipedia , lookup
Mains electricity wikipedia , lookup
Immunity-aware programming wikipedia , lookup
Two-port network wikipedia , lookup
Resistive opto-isolator wikipedia , lookup
Alternating current wikipedia , lookup
Buck converter wikipedia , lookup
Schmitt trigger wikipedia , lookup
Current mirror wikipedia , lookup
Transcript
Introduction to Sensor Technology Lecture One: Introduction to Physical Computing W.1.1. Physical Computing • Physical computing is about creating a conversation between the physical world and the virtual world of the computer. • The process of transduction or the conversion of one form of energy into another is what enables this flow. Pervasive Computing What is Interaction? • Interaction is an iterative process of listening, thinking and communicating between two or more actors. • • • • Input Output Processing Transduction/Understanding Skills for this Class • Electronics • Sensors and Actuators • Arduino Microcontroller and IDE Electronics Microcontrollers Wireless Sensor Networks Course Outline • Week One: intro to physical computing, electricity and electronics, sensor components, examples of work, Intro to the Arduino Board • Week Two: Using the breadboard to build simple circuits, Digital input and output, build a pressure sensor • Week Three: Analogue Input and Output • Week Four: hacking everyday objects, Building more complex circuits, interfacing with processing (and other things) • Week Five: Other Stuff Examples https://learn.adafruit.com/adalight-diy-ambient-tv-lighting Useful Links • www.makezine.com: Lots of different do it yourself electronics projects and forums • www.arduino.cc: Online support forum for arduino – includes tutorials, advice, forums and FTP posts as well as examples of projects other people have developed using arduino. • www.tigoe.org: Tom Igoe’s introduction to Physical Computing W.1.2. Introduction to Electricity • Electricity is the flow of tiny charged particles called electrons. • Electrons are present in all substances, but in some materials they are not free to move. These substances are known as insulators. • Substances which permit the flow of electricity are called conductors. To best describe how electricity and electric circuits work, we use what is called the ‘Water Analogy’ Water flowing = electrical Current (amps) Water Pressure = Voltage (volts) Size of Pipe = Level of Resistance (ohms) Power and Ground Connections • All electrical and electronic devices exploit the fact that electrons have a tendency to go from a point of greater electrical energy to a point of lesser electrical energy. • You provide a positive connection (greater energy or power) • A negative connection (lower energy, or ground) • A conductor through which the electrons flow. Circuits • A circuit is a closed loop containing a source of electrical energy (i.e. a battery) and a load (i.e. a light bulb) Circuits • There are three basic electrical characteristics that come into play in every circuit: • 1. Voltage • 2. Current • 3. Resistance • Voltage: The Relative level of energy between any two points in the circuit (for example between the power and ground). • Voltage is measured in volts • Current: The amount of electrical energy passing through any point in the circuit. • Current is measured in amperes, shortened to amps • Resistance: The amount that any component in the circuit resists the flow of current. • Resistance is measured in Ohms • Voltage current and resistance are all related and they all affect each other in a circuit. • The combination of current and voltage is called electrical power or wattage. It is measured in watts. The relationship is straight forward: watts = volts x amps (P=V*I) Ohm’s Law • To produce a balance between voltage, current and resistance we use Ohm’s law. Voltage = V Current = I Resistance = R Ohm’s Law • Ohm also expressed that power is related to voltage and current using this equation: • P=VxI • Power = Voltage x Current To lower the current flowing through a circuit we add resistance (i.e. Add a resistor to lower the current). If our light is not bright enough we must raise the power within the circuit – either the voltage applied across the wire or the current flowing through it. Electricity Vs. Electronics • Electronics is a subset of electrical circuits used to convey information. Electricity Power Supply: DC Vs. AC • There are two ways in which electrical power is usually supplied: • Direct Current: electrons flow one way through a wire or circuit. • Alternating Current: The electrons flow one way, then another, in a continuing cycle. W.1.3. Introduction to Sensor Technology One of the main principles behind physical computing is transduction, or the conversion of one from of energy into another A microphone is a classic transducer because it changes sound pressure into electrical voltage. Sensors and Actuators • Input transducers (sensors) such as switches and variable resistors, convert heat, light, motion and sound into electrical energy. • Output transducers (actuators) such as motors and buzzers, convert electrical energy into the various forms of energy the body can sense. W.1.4. Common Components in Sensor Technology • There are two different types of sensors: Digital and Analogue. • Digital = on/off or zero/one • Analogue = Any range of values, a continuous line Switches • Switches pass or interrupt the flow of electricity • A simple switch has two interchangeable leads. The leads are attached to two contacts inside the switch that can put them in contact with each other or be separated by the actions of the switch. Fixed Resistors • Resistors give electricity something - they convert electrical energy into heat. • Resistors are rated in ohms, indicating how much resistance they offer a circuit, and in watts, indicating the max power they can take. The value of a resistor will be written right next to its schematic symbol Resistors Variable Resistors • Variable resistors resist the flow of energy to variable degrees. These are very common transducers for analogue input. • • • • • Thermoresistors Photocells or Photoresistors Force sensitive Resistors Flex Sensors Potentiometer • Thermoresistors convert a change in heat to a change in resistance. • Photocells or Photoresistors change their resistance in response to changing light levels • Force sensitive resistors respond to a changing force exerted on them. These are often used in pressure sensors • Flex sensors change their resistance when they are bent at varying angles: they’re often used in interactive gloves • Potentiometer: The most common of all variable resistors is called a potentiometer or pot and this is what is behind every volume knob. • Capacitor: These store electricity to be released at a later point • They are rated by how much charge they store, which is called their capacitance measured in farads F. • Diodes: A diode only allows electricity to flow in one direction and not the other. • An LED is a light emitting diode that emits light in the process. The shorter leg is the cathode (negative), the longer LED is the anode (positive). Switching Devices: Transistors and Relays • Transistors and relays are switching devices. • Normal switches can be thrown by your finger, but these can be thrown by an electronic signal from your microcontroller. Wires Solderless Breadboard • A breadboard is a tool for holding the components of your circuit, and connecting them together. • It’s got holes that are a good size for wires and the ends of most components, so you can push wires and components in and pull them out without much trouble. W.1.5. Arduino • Arduino is an open source physical computing platform based on a simple input/output (I/O) board and a development language that implements the processing language (www.processing.org) • Arduino can be used to develop standalone interactive objects of connected to software on your computer, such as flash, Processing, VVVV or MAX/MSP • The boards can be assembled by hand or purchased preassembled; the IDE (integrated Development Environment) can be downloaded for free at www.arduino.cc Introduction to Arduino • Arduino is a multiplatform environment; it can run on Windows, Macintosh and Linux. • It is based on the Processing programming IDE, an easy to use development environment used by artists and designers. • You program it via a USB cable. • It is open source hardware and software • There is an active community of users so there are plenty of people who can help you. The Arduino Platform • Arduino is composed of two major parts: • the Arduino board; which is the piece of hardware you work on when you build your objects; • The Arduino IDE, the piece of software you run on your computer. You use the IDE to create a sketch ( a little computer program) that you upload to the board. The sketch tells the board what to do. The Arduino Hardware • The Arduino board is a small microcontroller board, which is a small circuit (the board) that contains a whole computer on a small chip (the microcontroller). • The board can be powered from your computer’s USB port, most USB chargers, or an AC adapter (9 volts recommended, 2.1mm barrel tip, centre positive) Arduino Hardware • 14 Digital IO pins (pins 0 -13) : these can be inputs or outputs, which is specified by the sketch you create in the IDE. • 6 Analogue In Pins (pins 0-5) These dedicated analogue inputs take analogue values (i.e. Voltage readings from a sensor) and convert them to a number between 0 and 1023. • 6 Analogue Out pins (pins 3, 5, 6, 9, 10 and 11) These are actually six of the digital pins that can be reprogrammed for analogue output using the sketch you create in the IDE. Arduino Deumilanove The Software • The IDE (Integrated Development Environment) is a special program running on your computer that allows you to write sketches for the arduino board in a simple language modelled after processing. • When you press the button that uploads the sketch to the board: the code you have written is translated into C language, and is passed to the avr-gcc compiler that makes the final translation into the language understood by the microcontroller. Arduino IDE Programming the Arduino: • The programming cycle on Arduino is basically as follows: • Plug the board into a USB port on your computer • Write a sketch that will bring the board to life • Upload this sketch to the board through the USB connection and wait a couple of seconds for the board to restart • The board executes the sketch that you wrote. Setting up the Arduino • To set up your Arduino plug the board into the USB port on your computer. • In the Tools menu of the IDE check: • Tools> Serial Port to select the correct COM* • Tools> Board to select the correct Board • * to find the correct COM you can also go to device manager and search under Ports (COM & LPT) for the correct port W.2.2. Blinking an LED • The LED blinking sketch is the first program that you should run to test whether your arduino board is working and configured correctly. • Your Arduino comes with an LED preinstalled – marked ‘L’. • We will also attach one to pin 13 • LEDs are polarised: negative = short lead (and often a flat edged side), positive = long lead Connect your LED as follows: (Positive pin 13, negative GRND) // Example 01: Blinking LED # define LED 13 // LED connected to // digital pin 13 void setup () { pinMode(LED, OUTPUT); // sets the digital // pin as output } void loop() { digitalWrite(LED, HIGH); // turns the LED on delay(1000); // waits for second digitalWrite(LED, LOW); // turns the LED off delay(1000); // waits for a second } Verify •Once you have typed in the code, you need to verify it to make sure it is correct. Press the verify button . If everything is correct you will see the message ‘Done compiling’ appear at the bottom of the Arduino IDE. Upload • At this point you can upload the sketch to the board, press the upload to I/O board button. • If this is successful you will see the message ‘Done Uploading’. IDE The Code Step by Step // Example 01: Blinking LED This is not a Comment // this is a line comment, good until the end of // a line /* This is a Block comment, everything in here will be ignored by the compiler */ ALWAYS COMMENT YOUR CODE • #define LED 13 • #define is like an automatic find and replace for your code: In this case, its telling Arduino to write the number 13 every time the word LED appears. • void setup() Your arduino code is made up of two main functions: setup() and loop(), Where setup() is the preparation, and loop() is the execution. { The curly bracket contains the block of code required for set up } • PinMode (LED, OUTPUT); • PinMode() tells Arduino how to configure a certain pin. Digital pins can be used either as INPUT or OUTPUT. In this case we need an output pin to control our LED, so we place the number of the pin and its mode inside the parentheses. • PinMode() is a function and the words or numbers specified inside the parentheses are what are called arguments. PinMode takes two arguments, the no. of the pin and whether it is specified as INPUT or OUTPUT. void loop() • loop() is where you specify the main behaviour of your interactive device. It will be repeated over and over again until you switch the board off. digitalWrite(LED, HIGH); • digitalWrite is able to turn on or off any pin that has been configured as an OUTPUT. It takes two arguments: which pin we are referring to (in this case LED, pin 13) and what state it should be in. • HIGH is on and LOW is off. delay(1000); • Delay produces a pause in the program between this line and the next step of code. • It takes an argument to specify the delay in milliseconds. digitalWrite (LED, LOW); delay (1000); • This instruction turns off the LED. Low = 0 volts/ OFF. • Again, we implement a second delay, so the LED will be off for a second. } • this closing curly bracket marks the end of the loop() function. ; semicolons • Every instruction (line of code) is terminated by a semicolon. It’s the compiler equivalent of a full stop. Pseudocode: • • • • • • • Turns pin 13 into an output enters a loop switches on an LED connected to pin 13 waits for a second Switches off the Led connected to pin 13 Waits for a second Goes back to the beginning of the loop Class Exercise: • Modify the code so that the LED is constantly on • Modify the code so that the LED is on for half a second and off for half a second • Modify the code to produce a variable pattern of blinks : i.e. on for a second, off for two seconds, on for ½ a second off for 5 seconds etc. Input & Output functions • digitalRead(pin) • Val =digitalRead(7); • digitalWrite(pin, HIGH/LOW) • digitalWrite(13, HIGH); • pinMode (pin, input/output) • pinMode (7, input); Using a PushButton to control the LED: • In our last example, our LED was our actuator, and our Arduino was controlling it. • What is missing to complete the picture is a sensor. • We’ll use a simple pushbutton sensor Pushbutton Pushbutton = two bits of metal kept apart by a spring, and a plastic cap that when pressed brings the two bits of metal into contact. When the bits of metal are apart, there is no circulation of current in the pushbutton. When we press it, we make a connection. We need: • • • • • • • a solderless breadboard wire, which we are going to need to cut and strip. One 10kohm resistor A momentary tactile pushbutton switch Connect your led to digital pin 13 as in the first exercise. Connect your push button to the breadboard. This circuit requires three wires: a power, a ground, and a wire to send the value of our sensor (pushbutton) to the arduino board. Push Button Circuit Logic circuit • A logical operation performed on one or more logic inputs to produce a single logic output • Our program looks for HIGH and LOW values as conditions to perform certain actions Floating Voltages • However, sometimes a digital pin will register a ‘floating voltage’ and the value read on the pin will not be perfectly stable. Pull-down Resistor • The basic function of a pull-up or pull-down resistor is to ensure that, given no other input, a circuit assumes a default value. • In this case it creates a default value for our circuit and pulls the line Low (0v) in the absence of another signal. • However, the pull-down resistor is weak enough that when something else pulls th wire towards 5v it will return a logic HIGH // Example 02: Turn on LED while the button is pressed #define LED 13 //the pin for the LED #define BUTTON 7 // the output pin where the pushbutton is connected int val = 0; // val will be used to store the state of the input pin void setup() { pinMode(LED, OUTPUT); //tell Arduino LED is an output pinMode(BUTTON, INPUT); // and BUTTON is an input } void loop(){ val = digitalRead(BUTTON); // read input value and store it if (val == HIGH) { digitalWrite (LED, HIGH); } else { digitalWrite (LED, LOW); } } // turn LED ON Code, Step by Step • #define LED 13 • #define BUTTON 7 • We have come across #define before. This time we have a second #define which replaces the input pin 7 with BUTTON. Variables • int val = 0; • Produces what’s called a variable • In order to store values and monitor input in our program we need to make a variable. This is a space in memory where a value can be stored. • int val = 0; creates a variable called ‘val’, which stores an integer, and initially assigns it the value of zero. void setup() as before PinMode (LED, OUTPUT); Pin 13 is an output PinMode(BUTTON, INPUT); This says that pin 7 is an input void loop() is the same as before DigitalRead() val = digitalRead(Button); • This line basically says, read the value of input pin 7 and store it in the variable called ‘val’. • A variable, as the name intimates, can be modified anywhere in your code. • In this line we assign ‘val’, which was initially given a value of 0, the value of pin 7. • Whatever arduino gets from the input ends up in the variable and will stay there until another line of code changes it. If Statement • An if statement is conditional – as in: if this condition is met: do this, else: do this. • After the if keyword, you write a ‘question’ or a condition to be met in parentheses, and if the condition is met, the first block of code will be executed, otherwise the block of code directly following else will be executed. if (val == HIGH) { digitalWrite(LED, HIGH); } else { digitalWrite(LED, LOW); • This is our first if statement. Note that the syntax of the statement is as follows: • if (condition to be met) { do this} else {do this}; Comparison Operators ‘==‘ • Notice here that we use the ‘==’ here instead of ’=’. • The former is used when two entities are compared, the latter when assigning/ initializing a variable. Pseudocode • Turn pin 13 into an output for our LED (actuator) • Turn pin 7 into an input from the push button (sensor) • Enter a loop • Read the value coming from pin 7 which is connected to our pushbutton and store it in the variable called ‘val’ • Check whether the input is high or low • if the input is HIGH, turn on the LED on • If the input is LOW, turn the LED off Try it • Make the circuit • Get the code from the website • scss.tcd.ie/~tayloral/Arduino.html Modifications: • This builds a circuit that requires you to hold the pushbutton down for as long as you require light. This is not very practical. We need to figure out how to make our button stick. • We therefore need to implement some form of memory in the form of a software mechanism that will remember that we have pressed the button and will keep the button on even after we have released it. • We need to create another variable to remember whether the LED has to stay on or off after we release the button… State Changes • In the next example we want to modify the code so that our pushbutton toggles the LED on and off. // Example 02B: Turn on LED while the button is pressed #define LED 13 #define BUTTON 7 //the pin for the LED // the output pin where the //pushbutton is connected int val = 0; // val will be used to store the state of the input pin int state = 0; // 0 = LED off while 1 = LED on void setup() { pinMode(LED, OUTPUT); //tell Arduino LED is an output pinMode(BUTTON, INPUT);// and BUTTON is an input } void loop(){ val = digitalRead(BUTTON);// read input value and store it if (val == HIGH){ state = 1 - state; } if (state ==1){ digitalWrite (LED, HIGH); // turn LED ON } else { digitalWrite (LED, LOW); } } Modifications int state = 0; • State is a variable that stores either 0 or 1 to remember whether the LED is on or off. • After the Button is released we initialize state to 0 (LED off). • Later in the program we read the current state of the button, and if its pressed (val == HIGH), we change the state from 0 to 1. • The first time the pushbutton is high, we want state to be 1. The next time we push it we want state to be 0 and turn our LED off. • To do this we have the line: state = 1 – state; • The new value of state will be equal to 1 – the old value of state. • We are using a small mathematical expression based on the idea that 1-0 is 1 and 1-1 is 0. if (state == 1) { digitalWrite(LED, HIGH); // turn LED ON } else { digitalWrite (LED, LOW); } • At the start of our program state is initialized to 0 and the LED is off. if we press the pushbutton, state is now equal to 1 minus the previous state( 0). 1-0 = 1. • The new value of state is 1. The LED lights up. • If we press the pushbutton again state is equal to 1 minus the previous value of state (1). • 1 – 1 = 0. State is equal to 0. The LED turns off. However... • The results are inaccurate because of the way we read the button. • We want to detect the exact moment when the button is pressed – that is the only moment in which we need to change state. • To do this we need to constantly store the value of val before reading a new one. • This allows us to compare the current position of the button with the previous one and change state only when the button becomes HIGH after being LOW. Modifications • old_val stores the previous value of the variable ‘val’ before reading a new one. • This monitors our state and compares it against the old value so that we monitor the exact moment the button is pressed. • Basically you have 4 possible conditions: • val = HIGH and old_val = LOW means you just now pushed the button • val = HIGH and old_val = HIGH means you're still holding the button • val = LOW and old_val = HIGH means you just let go of the button • val = LOW and old_val = LOW means the button is still depressed //Example 03B: Turn on LED when the button is pressed // and keep it on after it is released // with improvements #define LED 13 // the pin for the LED #define BUTTON 7 // the input pin for pushbutton int val = 0; val will store the state of the input pin int old_val = 0; // this variable stores the previous value of ‘val’ int state = 0; //0 = LED off and 1 = LED on void setup() { pinMode (LED, OUTPUT); PinMode(BUTTON< INPUT); } void loop() { val = digitalRead(BUTTON); //read input value and store it if ((val == HIGH) && (old-val == LOW)) { state = 1 – state; } old_val = val; // val is now old, lets store it if (state ==1) { digitalWrite(LED, HIGH); // turn LED ON } else { digitalWrite(LED, LOW); } } Boolean Operator • We come across a new operator here: ‘&&’ This is what’s called a Boolean operator: • These are used when you want to combine multiple conditions. • If this and this is true. ((val == HIGH) && (old-val == LOW)) • (If the current value of pin 7 is high and the previous value is LOW). Debouncing • This approach is not perfect due to another issue with mechanical switches. Pushbuttons are very simple devices: two bits of metal kept apart by a spring. When you press the button, the two bits of metal come together and electricity can flow. • In real life this connection is not perfect, especially when the button is not completely pressed, and it generates some spurious signals called ‘bouncing’. • When the pushbutton is bouncing, the Arduino sees a very rapid sequence of on/off signals. Debouncing • We want a simple technique to do debouncing. • We are going to add a 10 – 50 millisecond delay when the code detects a transition. Revision of Concepts Covered: • Arduino consist of two main functions: • Void setup() defines pins and serial communication • and void loop() functionality of code to be repeated over and over • Digital Input and Output functions: • digitalWrite() send value to a pin (13, HIGH) • digitalRead() Reads values from a pin • pinMode() Defines pin as Input or Output • Functions for Time • Delay() implements a delay in ms • Variables : a place in memory to store a value that may vary throughout • Variables need to be initialised at the start of a program i.e. Int val = 0; • Conditional Statements ‘if’ statement • If (this condition) {do this} else {do this} If (val == HIGH) {digitalWrite (LED, HIGH)} else {digitalWrite(LED, LOW)}; • • • • • Comparison operators == Boolean operators && Expressions state = 1 – state // comments /**/ {} curly brackets for setup, main and if (can be nested). • ; marks the end of an instructions Debugging with Serial • Serial.begin(9600); • Serial.print() • Serial.println () Exercises • Build the push button circuit. • Run Examples two and three • Modify the code so that your LED lights up on every third button push (i.e. 1 push the LED is off, two pushed the LED is off, three pushes the LED lights up, Four pushes the LED turns off, six pushes the LED lights up etc.) To do this you will need to create a new variable that can monitor the number of times your button has been pushed and use it to make decisions) • Any Questions: [email protected]