Add and read from a sensor

This week, I wanted to use a Piezo sensor. A piezo is a device that uses the piezoelectric effect to measure changes in pressure, acceleration, temperature, strain, or force by converting them to an electrical charge. I found this to be an interesting sensor because of its ability to produce electrical charge. I had been interested previously in storing this charge in a capacitor (some sort of energy storage mechanism combining an actuator with a battery), so I wanted to test out using a Piezo sensor.

I used a standard (6.3 kHz Standard Buzzer Element 0.787" Dia (20.00mm) 1 kOhms) piezo buzzer:

After looking over the data sheet for the ATSAMD11C microcontroller, I found that pin 5 (PA05) was an analog pin that I could hook up the piezo too. Additionally, a 1 Megaohm resistor is attachedi n parallel between the two legs (power and GND) of the piezo in order to limit the current entering the microcontroller, and any damage that the piezo might sustain from extra current. Below is an equivalent circuit (using an Arduino UNO), taken from the following tutorial site:

For code, I wanted to use Serial print and the Serial plotter in Arduino software to measure the voltage from the Piezo sensor. I used the following code:


// constants:
const int ledPin = 15;       // LED connected to digital pin 13
const int knockSensor = 5;  // the piezo is connected to analog pin 0
const int threshold = 100;   // threshold value to decide when the detected sound is a knock or not

// initialize variables:
int sensorReading = 0;  // variable to store the value read from the sensor pin
int ledState = LOW;     // variable used to store the last LED status, to toggle the light

void setup() {
  pinMode(ledPin, OUTPUT);  // declare the ledPin as as OUTPUT
  Serial.begin(9600);       // use the serial port
}

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);
  Serial.println("ADC reading:");
  Serial.println(sensorReading);

  // conversion of ADC readings
  float piezoV = sensorReading / 1023.0 * 5.0;
  Serial.println("Voltage");
  Serial.println(piezoV); // Print the voltage.

   // if the sensor reading is greater than the threshold:
  if (sensorReading >= threshold) {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
    
  } else {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(100);
  }
//  
}

  

I used the LED as a way to visually measure (without using the Serial port), when a threshold was being surpassed:

Below is a picture of the Serial Monitor. I was surprised by how small the values for the Piezo were, only varying from 0 - 100, even given that they were ADC values, meaning that in order to scale them to actual voltage values, they would need to be divided by the number of bits/total "buckets" and multiplied by 5 (the VCC):

Below is a picture of the Serial Plotter. You can see in the final two peaks when I pressed on the Piezo buzzer. The initial peaks are when the buzzer wasn't hooked up to the analog pin, meaning that the readings were just noise:

In order to adjust the threshold values, I used a multimeter to determine what ranges of voltage were coming out of the sensor. I noticed the voltage was at a maximum of 100 mV - 200 mV, and that they tended to come in "spikes" where sustained pressing only made the voltage dissipate. The peak in voltage occurred only when I initially pressed the buzzer:

Phone

(203) 507-0590 x12387