Week 6 - Electronic Input Devices

This week we learned about sensors, and I made two sensors. The first sensor uses capacitance to measure a physical quantity, and the second sensor is a microphone used to measure amplitude or loudness. I used the serial monitor and plotter in the Arduino IDE in order to calibrate each sensor and visualize the data.


Capacitive Sensor

My goal for this sensor was to create a tool that was able to measure physical quantities. In this case, I wanted to measure chocolate pieces! I'm not really a snack person so I still have a lot of leftover truffles from Valentine's Day, but I don't want my roommates to steal them! I decided to build a contraption that would measure whether or not some chocolates had been removed from the cup.

My first step was wiring the capacitive sensor. Using the steps we learned in class, I created the following capacitive-based sensor by attaching two copper electrodes (tape on circuitboard material) to wire attached with two 1M ohm resistors to make the signal stabler. I expect for the signal to increase when there is more force between the two copper plates, or when there is more chocolate in the cup!

Here is the image of the wiring for the capacitive sensor. One side of one resistor goes to GND, and the other end goes to 3.3V. Then one wire connects where the two resistors meet to the analog A0 pin, which is also in the same row as the wire connected to one of the copper plates. Finally, the second copper plate is connected to digital pin 4.

Now that my sensor is wired, I'm ready to start building my truffle detector! I simply taped the copper plates to the cup, applying the tape so the copper part was facing inwards towards the cup.

After learning more about capacitance, I decided to first calibrate my sensors using water. I first measured the values given an empty cup, and then measured the values given a full cup and a half filled cup of water.

I used code from Robert Hart's website to program my capacitive sensor to transmit-receive across space between two resistors.


                //  rx_tx02  Robert Hart Mar 2019.
                //  Program to use transmit-receive across space between two conductors.
                //  One conductor attached to pin4, one to A0
                
                //  Optionally, two resistors (1 MOhm or greater) can be placed between 5V and GND, with
                //  the signal connected between them so that the steady-state voltage is 2.5 Volts.
                
                //  Signal varies with electric field coupling between conductors, and can
                //  be used to measure many things related to position, overlap, and intervening material
                //  between the two conductors.
                

                int read_high;
                int read_low;
                int diff;
                long int sum;
                int N_samples = 1000;    //Number of samples to take.  Larger number slows it down, but reduces scatter.

                int analog_pin = A0;
                int tx_pin = 4;

                void setup() {
                    pinMode(4,OUTPUT);      //Pin 4 provides the voltage step
                    Serial.begin(115200);
                }

                void loop() {

                    sum = 0;

                    for (int i = 0; i < N_samples; i++){
                        digitalWrite(tx_pin,HIGH);              // Step the voltage high on conductor 1.
                        read_high = analogRead(analog_pin);     // Measure response of conductor 2.
                        delayMicroseconds(1000);                // Delay to reach steady state.
                        digitalWrite(tx_pin,LOW);               // Step the voltage to zero on conductor 1.
                        read_low = analogRead(analog_pin);      // Measure response of conductor 2.
                        diff = read_high - read_low;            // Desired answer is the difference between high and low.
                        sum += diff;                            // Sums up N_samples of these measurements.
                    }
                    
                    Serial.println(sum);

                }
            

I then made some plots using the Arduino IDE (Tools > Serial Plotter) in order to see the various values at the different settings.

The measured values of the empty cup ranged from about 18000 to 21000. We can say that the average value when the cup is empty is about 19500 and use this value as a baseline for our future values once the cup is filled.

The measured value of a half-filled water cup is about 309000, so significantly more than an empty cup.

The measured values of a full water cup is about 314000, so slightly more than a half-filled cup.

I then emptied the cup again and found that the measurements were slightly different than those that I got when I first measured the sensor values using the empty cup, it seems like capacitive sensors can be very sensitive!

Now that I'm familar with the values and can use my original value of about 19500 as a baseline, we can measure chocolate in the cup. My goal is to figure out if some chocolate pieces have been removed from the cup. This sensor is suitable for this goal because I don't need to know exactly how many pieces have been taken, I just want to know if the cup is full or has less chocolate than the expected 20 pieces.

Here are the measured values when all 20 pieces of chocolate are in the cup. I get an average value in the 27000 to 28500 range.

I wondered, what if someone takes one piece of chocolate? It didn't look like there was much change in the capacitance value, which ranged around 27000 to 28000 when the cup held 19 truffles.

What if I removed 5 pieces, leaving only 15 in the cup? It looks like there's a slight drop in value! Now the value averages about 26000. Now I'll know if someone steals a few of my chocolates!

Let's try removing more pieces so that the cup is half full (10 pieces left). Most values are around 24000:

Here's the reading at five chocolate pieces which averages around 22000:

Everytime I removed five pieces, there was a significant drop in the capacitance value. Looks like the sensor is working well and can detect if a few pieces are taken from the jar! Now I’ll know next time my roommates try messing with my chocolate...


Microphone Sensor

Wiring the microphone was pretty straightforward! The SPW2430 board uses an analog MEMS microphone and is fairly simple to hook up. I connected GND to GND, Vin to 3.3V, and the analog audio waveform comes out of the DC pin so I have the DC pin connected pin A1 on the Metro M0 Express board.

Here's the code that I used to test the microphone:


                const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
                unsigned int sample;

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


                void loop() {
                    unsigned long startMillis= millis();  // Start of sample window
                    unsigned int peakToPeak = 0;   // peak-to-peak level

                    unsigned int signalMax = 0;
                    unsigned int signalMin = 1024;

                    // collect data for 50 mS
                    while (millis() - startMillis < sampleWindow)
                    {
                        sample = analogRead(1);   //reading DC pin from pin A1
                        if (sample < 1024)  // toss out spurious readings
                        {
                            if (sample > signalMax)
                            {
                                signalMax = sample;  // save just the max levels
                            }
                            else if (sample < signalMin)
                            {
                                signalMin = sample;  // save just the min levels
                            }
                        }
                    }
                    peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude

                    Serial.println(peakToPeak);
                }
    
            

In order to use the sensor, I measured the output values given a quiet environment. Even without any noise, the value seemed to output an average value of 9.0.

The values spiked up to measurements as high as 350 when I clapped over the microphone sensor.

I also tried singing into the microphone, but it did not reach amplitude values anywhere near the claps with a maximum value of around 17 (only 8 more than the silent baseline).