7 Review and Sensor in Arduino
7 Review and Sensor in Arduino
Almost all systems that use physical computing will have some form of output
• Variable Types:
ANALOG
INPUTS
SPST
pinMode(3, INPUT);
momentary
Pull up resistor?
1
PD3
0
PWM
Fading in and Fading Out
(Analog or Digital?)
• A few pins on the Arduino allow for us to modify the output to mimic
an analog signal.
•analogWrite(pin, val);
•
•pin – refers to the OUTPUT pin
(limited to pins 3, 5, 6, 9, 10, 11.) –
denoted by a ~ symbol
void doSomething()
{
Serial.print("2 second tick: millis()=");
Serial.println(millis());
}
void doAfter()
{
Serial.println("stop the led event");
t.stop(ledEvent);
t.oscillate(13, 500, HIGH, 5);
}
Sensor
What is a sensor?
• A device that receives a stimulus and responds with an electrical signal.
• A special type of transducer (device that converts one type of energy
into another
Common Sensors
• Mechanical
• Accelerometers
• Gyroscopes
• Optical
• Photodetectors
• Infrared
• Semiconductor
• Gas
• Temperature
• Magnetic
Example: Simple temperature sensor
• Error: the difference between the measured value and true value
• Systematic errors are reproducible inaccuracies that can be corrected
with compensation methods
• Interference errors
• Operator errors etc.
• Random error
• Noise
Sensor Characteristics (4/4)
• Hysterysis: the difference in output between the rising and falling
output values for a given input
Example: Smoke sensor (1/2)
• An MQ-2 smoke sensor reports smoke by the voltage level it puts out.
• The more smoke there is, the higher the voltage.
• built-in potentiometer for adjusting sensitivity
• Three pins:
• Vdd input
• Ground input
• Analog output
Smoke sensor (2/2)
const int smokePin = 54; //sensor input
void setup() {
pinMode(smokePin, INPUT);
Serial.begin(9600);
}
void loop() {
int smoke_level = analogRead(smokePin); //read sensor
Serial.println(smoke_level);
if(smoke_level > 120) { //calibrate accordingly
Serial.println("Smoke detected");
}
delay(100); // ms
}
References
46
Example
void loop() {
sum = 0; //green is error control, red is signal conditioning
for(int i=0; i<10; i++){
int sum+ = analogRead(A0); //average of 10 temp measurements
delay(1650); //sensor response time
}
int value = sum/10; //value is a raw data element
//with a 10-bit ADC we get a 0 < value < 1023 for a 0 – 3.3V range
//sensor gives voltage between 100mV and 1750 mV
int mV = map(value,0,1023,0,3300); //returns mVs
if (mV < 100 || mv > 1750) // value error
…
else
int temp = map(mV,100,1750,-40,125); //temp is the conditioned signal
47
Actuators
Actuators
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
int val = 180; // variable to control servo
myservo.attach(9); // pin 9 is a PWM pin
}
void loop() {
myservo.write(val); // constant servo speed
delay(15); // waits for the servo to get there
}
Stepper Motors
• motor controlled by a series of electromagnetic coils.
• The center shaft has a series of magnets mounted on it
• the coils are alternately given current or not, creating magnetic
fields which repulse or attract the magnets on the shaft,
causing the motor to rotate.
• allows for very precise control of the motor. it can be turned in
very accurate steps of set degree increments
• two basic types
• unipolar
• bipolar
Example: Unipolar stepper motor
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60); //actually sets the delay between steps
}
void loop() {
// step one revolution in one direction:
myStepper.step(stepsPerRevolution);
delay(500);
}