PROJECT WITH PIEZO AND FAN

flaminiab

Member
I am building a system that will turn on a computer fan for 10 S in response to vibrations. Vibration is detected with piezo electric elements mounted in . The signal is sent to a teensy that controls a 12V fan. I set a threshold to determine when the fan should turn on, It turns off after 10 sec. I have 2 questions:
1)What is the unit of threshold on teensey? On my tests I entered values such as 300 or 250
2) when I trigger the piezo, it works sometimes; sometimes not so piezo stimulation doesn't reliably turn on the fan as expected. Sometimes it turned on without obvious trigger and the piezo value read is not 0 when you don't touch the piezo but it fluctuate between 150 and 300.

Here you can find a brief explanation fo the circuit: The system consists of five piezoelectric sensors connected in parallel and placed on the floor of the cage. These sensors are connected via a BNC connector, with the red wire attached to pin A0 and the black wire connected to the ground pin of the Teensy.
The black wire from the BNC is connected in series to a 10 kΩ resistor,which is then connected in series to the ground of the Teensy; the reason is to limit the current
flowing through the circuit. The fan has two pins: the power pin (red wire) and a ground pin (black wire). The red wire is soldered to the red wire of the 12V AC adapter, while the black wire is connected to the ground of the Teensy.
Since the fan requires 12V but the Teensy supports only up to 3.3V, a MOSFET is used to control the fan. The gate of the MOSFET is connected to the PWM pin 3 of
the Teensy, the drain is connected to the fan’s black wire and the source is connected to the ground of the 12V power supply, which is also tied to the ground of the Teensy.
To ensure proper operation, a 10 kΩ pull-down resistor is placed between the gate and ground of the MOSFET. This resistor purpose is to ensure that the MOSFET remains turned off when the control signal (from the Teensy) is not actively driving the gate.
The system is controlled by a code written in Teensyduino, which monitors the piezoelectric sensors and activates the fan when a certain threshold is exceeded. You can find the script to commend the teensy attached.
1727153030703.png

1727153063345.png

1727153131626.png
 
Can't really figure out your piezo circuit from that description - do you have a schematic? Piezo's are capacitive and shouldn't be left floating, normally a high value resistor(s) is needed to define the quiescent voltage from them. Usually in the megaohm range.
 
Quick comments on possible issues here.

1: Restructure your program to not use delay(). You will miss activity during the delay time! I'd recommend adding a bool variable to remember whether the fan is on, and an elapsedMillis variable to remember how long it has been on. In the case where you turn on the fan, set the bool to true, and set the elapsedMillis to zero. Then add another check in loop() for the case where the fan is on and elapsedMillis > 10000. In that case, turn off the fan and of course set the bool to false. Simply delete the final delay(). You want loop() to always be checking the sensor input as rapidly as possible, so you don't miss anything!

2: This circuit is (probably) not safe. You could risk damage to Teensy A0 pin, possibly even destroy the entire board if the sensor sends too strong of a signal. You want at least a series resistor, a clamping diode to prevent negative voltage, a pull-down resistor, and perhaps 1 more series resistor to limit current if positive voltage. I'll write more on this later. Or maybe others will comment....

3: This type of piezo sensor is extremely sensitive to heat damage during soldering. Best to buy them with the wires already attached, and solder only to the ends of those wires. Soldering directly to the sensor will almost certainly reduce its performance.

4: Not all mosfet transistors are created equal. I didn't see the part number you're using. You want one which has a low enough gate voltage to turn on reliable with only 3.3V. You also want one that has a low "total gate charge", or if it doesn't, you need a series resistor to limit the current from Teensy into the gate. You should also have a high speed clamping diode in parallel with the fan, as inductive loads can create a high voltage spike when the transistor turns off.
 
I repeat the piezo sensor is a capacitor so you need to define the DC voltage across it, otherwise it will be at an arbitrary voltage. If for instance you connect two 1M resistors to A0, one to GND and one to Vcc, you'll define the resting voltage at mid-rail, giving the ability to detect pulses of either polarity.
You already have a series resistor to ground so that's already covered I think.
 
Mark has a good point about the AC nature of this piezo sensor. If you know for certain the polarity of the signal, you could use a circuit which clamps the negative signal to (nearly) zero and allows the positive signal to reach Teensy's ADC pin. But without that perfect knowledge (or careful testing using an oscilloscope and the actual mechanical input you're trying to detect), you're probably better off building a circuit which will give Teensy's ADC pin 1.65V when the sensor is not detecting any mechanical input.

To answer your original question "What is the unit of threshold on teensey?", analogRead() gives you a number from 0 to 1023 which corresponds to 0 to 3.3V at the pin. What voltage you get at the pin depends on the circuitry you connect.

Here's one quick idea of how you might try connecting this piezo sensor.

1727258809457.png


This circuit uses 5 resistors and 2 diodes. The first (left side) 1K and 100K resistors create the 1.65 volt level which Teensy will receive when you have no mechanical stimulation of the sensor. The 10K resistor and 2 diodes limit the signal to about 3.9 volts if the sensor gives a large positive signal, and to about -0.7 volts if it outputs a large negative signal. The 1K resistor tries to limit the current which might flow into Teensy's internal diodes in those cases.

With this circuit, you should read approximately 512 normally. When the sensor hears vibration, you will get numbers higher and lower. So your code which tries to detect activity might look like this:

Code:
if ((piezoValue > 512 + threshold) || (piezoValue < 512 - threshold)) {

Hopefully this helps you to understand how to better connect the sensor, and how your code ought to read it. Again, as I tried to explain earlier, you need to eliminate the delay() usage and restructure your program to always read the sensor at high speed if you want to craft a system which is responsive to all inputs.

One final point I'd like to make is how easy a simple schematic is to show on this forum. For this sake of this message (admittedly I did not have much time available earlier) I just picked up a pen and quickly drew this diagram on paper and used a camera to make this photo. You can do the same. Learning a CAD program takes time, but simply using pen & paper with a camera is so quick and easy.
 
It doesn't look like Teensy header pins are soldered on "analog row". Intermittent contacts another contributor to false readings.
 
Thank you so so much for the advice. I'm trying to replicate the circuit on the breadboard. Is the code correct now?

1727288980573.png
 
My take on a basic piezo circuit, relies on 10k's being high enough to protect the input, low enough to prevent too much analog pin cross-talk:
piezo.png

It might not ideal with long leads to the piezo element though - might be better with a stiffer potential divider and some extra resistance in series with the sensor.
 
Yes, A0 pin. Or any of the analog input pins. Any can you as long as you specify the pin in the code where you actually connected the wire.

Code looks better, at least the 36 lines I can see.

When you post code here, click the </> button. A popup dialog appears. Then in Arduino, use Ctrl-A (select all) and Ctrl-C to copy to clipboard. Then in the popup dialog, use Ctrl-V to paste. This way allows showing all your code and it preserves the formatting, which would be lost if you just paste into your message.

I also want to emphasize, this is your project. I can give you some basic advice and answer questions about Teensy and sometimes about electronics in general (items not sold by PJRC). But I can't always give you every detail, like which pin, review every line of code, or say exactly which keys to press on your keyboard, as in this message. Sometimes you will need to figure out these little details. Especially when it comes to your code, you will need to run it and see how it really works and try to resolve any problems.
 
Yes, thank you for your help. I tried the configuration done by Mark and it's working. I will try even yours in the next days. Thank you so much again
 
Back
Top