Using a Piezo


20 posts
by lolouk44 » Sat Oct 20, 2012 11:07 pm
Hi all,

I'm using my Pi as a CCTV (with Lavrsen's motion and a webcam).
I managed to connect wires to the bolt of my door to only activate motion detection when the door is locked.
What I'm looking to do now is to get an audio feedback when I activate the alarm.
I have a piezo transducer but can't seem able to drive it to get sound.
Has any one managed to get a piezo working on a Pi with bash programming?
I haven't programmed in python yet and ideally don't want to recode my bash script.

Thanks
Posts: 25
Joined: Sat Oct 20, 2012 11:01 pm
by Grumpy Mike » Mon Oct 22, 2012 8:55 am
I have a piezo transducer

There are two sorts of these:-
1) The one you just need to supply a voltage to and it will make a noise. This sort you could connect directly to a GPIO pin.

2) The one that is more like a speaker that you need to send a succession of high and low signals to make a noise. This sort you could connect to the phones jack.
User avatar
Posts: 783
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
by jojopi » Mon Oct 22, 2012 12:23 pm
If it is the clicking capacitor type then you can also drive it with a square wave using pin P1-12 (GPIO18) in mark-space PWM mode. To do this from bash you can use the "gpio" program that comes with wiringPi: https://projects.drogon.net/raspberry-p ... d-install/
Code: Select all
#! /bin/bash

tone () {
  local note="$1" time="$2"
  if test "$note" -eq 0; then
    gpio -g mode 18 in
  else
    local period="$(perl -e"printf'%.0f',600000/440/2**(($note-69)/12)")"
    gpio -g mode 18 pwm
    gpio pwmr "$((period))"
    gpio -g pwm 18 "$((period/2))"
    gpio pwm-ms
  fi
  sleep "$time"
}

tone 60 0.2
tone 55 0.1
tone 55 0.1
tone 57 0.2
tone 55 0.2
tone 0 0.2
tone 59 0.2
tone 60 0.2
tone 0 0
User avatar
Posts: 1953
Joined: Tue Oct 11, 2011 8:38 pm
by lolouk44 » Tue Nov 06, 2012 8:38 pm
Hi Guys. For some reason I didn't get alerted by emails of the replies. Sorry for the delay.

@jojopi, I'll try your suggestion thanks.
I'll post back in any case.

Thanks again.
Posts: 25
Joined: Sat Oct 20, 2012 11:01 pm
by lolouk44 » Tue Nov 06, 2012 9:29 pm
@jojopi

Thanks a bunch.
Took me a while to realise that copy / paste using the RPi built in web browser does not copy the spaces correctly. Kept giving me error messages while trying to run.
I have manually overwritten all the spaces and It works like a dream.

Too bad the piezo is not loud enough, but as a proof of concept I love it.

Thanks again, you made my day :)
Posts: 25
Joined: Sat Oct 20, 2012 11:01 pm
by DanR » Fri Jan 18, 2013 1:23 pm
You need to drive the piezo with an alternating current, the easiest and simplest quick and dirty method is to drive it across a logic inverter such as pins 1 and 2 of a 74HC14, just connect pin 1 of the chip to your pi output and the piezo across ppins 1 and 2.

HTH, Dan
Posts: 7
Joined: Fri Jan 18, 2013 1:20 pm
by SiriusHardware » Wed Jan 23, 2013 12:06 am
Another point worth knowing about 'simple' Piezo buzzers (the ones which are just a sounder element, not the ones with a built in oscillator) is that they usually resonate very strongly at a particular frequency - so to get the maximum sound level out from one you need find out what the peak frequency of the device is and drive it with a waveform at that frequency. Conversely, if you don't want the sounder to be particularly loud, then deliberately drive it at some other frequency.

Another important thing to know about Piezos is that they, like some other audio transducers, work in reverse. You normally use them by applying an alternating voltage to them: It makes the element flex in sympathy with the voltage and that produces sound.

But also... a loud sound in the vicinity of (or an impact upon) the element will cause it to vibrate, which in turn will cause it to GENERATE voltage.

In extreme cases the voltage generated can be enough to damage whatever electronic output pin or driver device is connected to it.

It's therefore advisable to have some kind of overvoltage suppression (typically a pair of opposed zener diodes in series across the sounder) to limit the maximum voltage which can develop across it.
Posts: 439
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK
by mahjongg » Wed Jan 23, 2013 12:33 am
simplest way, put one lead on a GPIO, and another on GND, send a square wave to the GPIO pin with a frequency roughly equal to the resonant frequency of the Piezo, the frequency is very important, use any other frequency and the sound output will be much lower.

The easiest way to output a square wave is to use the GPIO that is able to output a PWM signal.
It would also be easier to find the resonant frequency if you do not know it. Use a PWM signal with a 50% duty cycle, and an adjustable frequency between 1 and 10KHz.

If you want a louder signal, drive the piezo "balanced" in-between two outputs that are the inverse of each other, (using an inverter), so that if one output goes low he other goes high.
User avatar
Forum Moderator
Forum Moderator
Posts: 4888
Joined: Sun Mar 11, 2012 12:19 am
by SiriusHardware » Wed Jan 23, 2013 6:43 pm
lolouk44 wrote:@jojopi

Too bad the piezo is not loud enough, but as a proof of concept I love it.

Thanks again, you made my day :)


If it isn't very loud you are almost certainly just driving it at the wrong frequency. If the sounder is cased and has a manufacturer's part number, try googling your way to a datasheet which should tell you the peak / resonant frequency of the sounder. If you hit it with the correct frequency it will be at least 4-5 times louder than if you hit it with the wrong frequency. You'll be amazed at the difference it makes.

I was recently given a 'Swan Teasmade' (A combined bedroom alarm clock and tea maker) to look at because the owner is an elderly lady and quite deaf. The alarm sounder was a very small piezo unit confined behind a watertight screen (necessary to keep steam and water away from the electronics). It was hardly audible to anyone with normal hearing who was awake, never mind a deaf person who was asleep, so she kept waking up to cold tea.

First step was to just replace the small sounder with a much larger one from a discarded smoke detector. To my surprise, it was hardly any louder. What I then discovered was that the drive waveform supplied to the original sounder was 3.9Khz, but that the resonant frequency of my new sounder was 3.1Khz.

Consequently, it was very quiet when just dropped in as a direct substitute.

The next step was to lash up a 3.9Khz tone detector / 3.1Khz tone generator using a small 8-pin PIC chip. That detects the 3.9Khz tone generated by the teasmade and, whenever that tone is present, sends a push-pull output drive at 3.1Khz to the big Piezo. Result: Ear splittingly loud audio. Once wrapped up inside the watertight control unit though, the audio is at just about the right level: Rather too loud for someone with normal hearing, but perfect for someone who's a bit deaf.

So the moral of the story is: For simple Piezo sounders, the frequency at which you drive them is critical.
Posts: 439
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK
by paulie » Wed Jan 23, 2013 7:49 pm
SiriusHardware wrote:But also... a loud sound in the vicinity of (or an impact upon) the element will cause it to vibrate, which in turn will cause it to GENERATE voltage.

In extreme cases the voltage generated can be enough to damage whatever electronic output pin or driver device is connected to it.


A poster in another forum was looking for some sort of pressure sensor
(strain gauge rather than fluids).
Would piezos work for this application?
Posts: 177
Joined: Thu Jan 19, 2012 6:51 pm
by SiriusHardware » Wed Jan 23, 2013 9:09 pm
To tell you the truth, I'm not sure whether they generate constant voltage in response to constant pressure. If they did, it might be a useful avenue to explore.

More likely, they only generate voltage during changes in applied force (that being the case, they would generate a burst of voltage when you squash them, then nothing for a while... then another burst of voltage when you let them relax) - not really what you want for a strain gauge, which needs to output a constant signal proportional to the amount of stress being sensed.

(But I could actually be entirely wrong about what happens).
Posts: 439
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK
by gordon@drogon.net » Thu Jan 24, 2013 9:44 am
An old post that has bubbled to the top again ;-)

FWIW: I have been driving Piezeo sounders (the little flat disc type) from the Pi and they're remarkably loud too. (and that's without doing the push-pull method). I've also stuck a 64Ω speaker on a GPIO to 0v too (although I don't recommend it for long term use!) and it was fine.

The trick with the flat disc types is to fix them to a larger thin surface with e.g. superglue...

Originally I was after that olde-Apple II or Stylophone experience, but to drive them, I recently wrote a softTone module for wiringPi - it works very similar to the softPWM module and can generate a frequency from 0 to 5KHz which is good enough for most simple tones.

In theory it can drive multiple pins, but I've not tested it - so you could have 2 for stereo, or many for a polyphonic (square wave) experience...

There's a demo program in the latest wiringPi, but it looks like:

Code: Select all
#include <stdio.h>

#include <wiringPi.h>
#include <softTone.h>

#define PIN 3

int scale [8] = { 262, 294, 330, 349, 392, 440, 494, 525 } ;

int main ()
{
  int i, j ;
  char buf [80] ;

  if (wiringPiSetup () == -1)
    return 1 ;

  softToneCreate (PIN) ;

  for (;;)
  {
    for (i = 0 ; i < 8 ; ++i)
    {
      printf ("%3d\n", i) ;
      softToneWrite (PIN, scale [i]) ;
      delay (500) ;
    }
  }
}


-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1487
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by mahjongg » Thu Jan 24, 2013 9:59 am
SiriusHardware wrote:To tell you the truth, I'm not sure whether they generate constant voltage in response to constant pressure. If they did, it might be a useful avenue to explore.

Logically they do not, as a constant pressure (without movement) doesn't generate energy, and energy generation is needed if they are to generate a detectable voltage (that means if you can measure it, and thus by nature load it with a current flow). See it as a small capacitor that will gain some charge when mechanically distorted.
User avatar
Forum Moderator
Forum Moderator
Posts: 4888
Joined: Sun Mar 11, 2012 12:19 am
by SiriusHardware » Thu Jan 24, 2013 6:09 pm
mahjongg wrote:
SiriusHardware wrote:To tell you the truth, I'm not sure whether they generate constant voltage in response to constant pressure. If they did, it might be a useful avenue to explore.

Logically they do not, as a constant pressure (without movement) doesn't generate energy, and energy generation is needed if they are to generate a detectable voltage (that means if you can measure it, and thus by nature load it with a current flow). See it as a small capacitor that will gain some charge when mechanically distorted.


I did wonder if this was one of those things which are theoretically possible, but physically impossible. They are very high impedance devices (basically a sliver of rock between two electrodes) and so I can see it being possible that any voltage generated by flexing the device would simply stay there... as long as you didn't try to measure it or use it in any way, since doing so would cause current to flow and discharge any stored charge :-)
Posts: 439
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK
by techpaul » Thu Jan 24, 2013 8:11 pm
What is the part number of the piezo or link to datasheet?

I have worked with piezo transducers that work in the Ultrasonic range 100 to 180 kHz, that was to stir beer :D

They had some substantial power requirements.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1509
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by gordon@drogon.net » Thu Jan 24, 2013 9:22 pm
techpaul wrote:What is the part number of the piezo or link to datasheet?

I have worked with piezo transducers that work in the Ultrasonic range 100 to 180 kHz, that was to stir beer :D

They had some substantial power requirements.


The ones I'm on about are like these: http://www.maplin.co.uk/piezo-transducers-3202

little flat discs that squeak when you put a square wave over them...

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1487
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by techpaul » Thu Jan 24, 2013 9:35 pm
gordon@drogon.net wrote:little flat discs that squeak when you put a square wave over them...

-Gordon

They are nearly all driven with square waves only amplitude (power) and frequency varies... Ones I was using had a pint glass stood on it.

Wonder what original poster is using.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1509
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by thelazymastermind » Mon Feb 18, 2013 3:49 pm
jojopi wrote:If it is the clicking capacitor type then you can also drive it with a square wave using pin P1-12 (GPIO18) in mark-space PWM mode.


Thanks for the code! I managed to make a semi-functional keyboard script out of it, except for a PC speaker.

viewtopic.php?f=38&t=34155
Posts: 2
Joined: Mon Feb 18, 2013 1:51 am
by 0xFF » Sun Apr 07, 2013 10:29 am
jojopi wrote:If it is the clicking capacitor type then you can also drive it with a square wave using pin P1-12 (GPIO18) in mark-space PWM mode. To do this from bash you can use the "gpio" program that comes with wiringPi: https://projects.drogon.net/raspberry-p ... d-install/
Code: Select all
#! /bin/bash

tone () {
  local note="$1" time="$2"
  if test "$note" -eq 0; then
    gpio -g mode 18 in
  else
    local period="$(perl -e"printf'%.0f',600000/440/2**(($note-69)/12)")"
    gpio -g mode 18 pwm
    gpio pwmr "$((period))"
    gpio -g pwm 18 "$((period/2))"
    gpio pwm-ms
  fi
  sleep "$time"
}

tone 60 0.2
tone 55 0.1
tone 55 0.1
tone 57 0.2
tone 55 0.2
tone 0 0.2
tone 59 0.2
tone 60 0.2
tone 0 0


Hello,

I connected piezo beeper to P1-12 (GPIO18) PIN.
It beeps as loud as alarm clock ;-)

The problem is when piezo beeps, the beep sound persist in analog audio output (headphone jack).
It also confuses xbmc media player - it stops playback when piezo beeps.

I assume there is common clock generator for PWM and audio PCM ?

How can I restore audio playback after beeping with piezo?

Best regards
Piotr
Posts: 115
Joined: Tue Nov 20, 2012 7:52 pm
by gordon@drogon.net » Sun Apr 07, 2013 10:40 am
0xFF wrote:The problem is when piezo beeps, the beep sound persist in analog audio output (headphone jack).
It also confuses xbmc media player - it stops playback when piezo beeps.

I assume there is common clock generator for PWM and audio PCM ?


Yes - or to be more precise it's the PWM generator. There are only 2 PWM channels in the Pi - one for left and one for right via the 3.5mm jack - but one is also BCM_GPIO 18, so when you use the PWM pin on the GPIO connector, then it upsets the audio systems..

How can I restore audio playback after beeping with piezo?

Best regards
Piotr


I don't know... Possibly unloading & reloading the sound modules?

An plan B might be to write a little program that uses the as yet undocumented softTone library inside wirrngPi.. See examples/tone.c for an example...

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1487
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK