Course Description

Art B2050, Fall 2013, DIAP at CCNY
A survey of modern electromechanical construction. Lessons interweave hardware, firmware, software and networking. Specific focus on paper and cardboard prototyping.

Wednesday, October 30, 2013

Week 9, 10/30: Sensors

Sensors come in many shapes and sizes. Understanding what types of sensors are around at what price points will help you shape your projects. The thing most of you were interested in sensing was the presence and proximity of people, so here are two sensors for that purpose. A piezo sensor detects vibration— a very crude version of a microphone. A ping sensor emits and detects an ultrasonic pulse; the time it takes the pulse to return to the sensor tells you how far away the closest object is. 

Sensors typically have two or three terminals. Every sensors needs power, ground, and at least one signal wire. With two-terminal sensors, like photoresistors and piezos, a voltage divider is required. The ground terminal connects to ground; the positive terminal connects to the signal input pin and also to a resistor; the resistor connects to the source voltage.



Piezo Code
int ledPin = 13;
int knockSensor = 0; 
byte val = 0;
int statePin = LOW;
int THRESHOLD = 100;

void setup(){
  pinMode(ledPin, OUTPUT); 
  Serial.begin(9600);
}

void loop(){
  val = analogRead(knockSensor); 
  if (val >= THRESHOLD) {
    statePin = !statePin;
    digitalWrite(ledPin, statePin);
    Serial.println("Knock!");
  }
  delay(100);
}

Ping Code
const int pingPin = 10;

void setup() {
  Serial.begin(9600);
}

void loop(){
  Serial.println(ping());
  delay(100);
}

int ping(){
  long duration, cm;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  return cm;
}

long microsecondsToCentimeters(long microseconds){
  return microseconds / 29 / 2;
}


No comments:

Post a Comment