Driving Piezo with PIC

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Driving Piezo with PIC

Post by nmindana »

Just wondered if anybody has any ideas, about driving a piezo, from a PIC to produce a loud tone.
The sound from my design is hardly audible. I used a transistor to amplify but still with poor results.

Any suggestions welcome.

Indana

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Driving Piezo with PIC

Post by Jordy101091 »

It depends,

do you want to use you peizo buzzer to produce a continues tone.
Also you need to know if your buzzer works with DC or AC.

I case your peizo buzzer works with AC. you can make a multi vibrator that produces a block signal. You can also made a block signal using a standard LM742(i think but you need a duel supply + common -). But as you know AC has the characteristics to change polarity over a period of time.

Image

But what you want is a block signal to drive you Peizo Buzzer, If you build a multi vibrator you would only have block of 0V > 5V > 0V > 5V and so on. By putting a elco of about 1uf on the output you will get a alternating block signal that goes from 0V > +2,5V > 0V > -2.5V > 0V

You can play whit the values of the capacitors to change the pitch of the sound.
Image

http://en.wikipedia.org/wiki/Multivibrator

I hope you can do something with this.

By the way the output of this multi vibrator is the plus pole of capacitor C2

Regards Jordy

TIP: If you have NI multisim 10 you can simulate this circuit for better understanding
the will to learn, should not be stopped by any price

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Hi Thanks,

Sorry I meant Piezo transducer and not a buzzer, I need to get a pulsed tone.
I read somewhere you have to hit the resonant frequency of the piezo for max output.
This is something which has not been discussed in flowcode before.

Any one with experience using Inductors ?.
I really would appreciate a working example.

Indana

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

If it was me then First, I would determine resonant frequency, by looking on the data sheet or site where you purchased Piezo transducer from. Then I would produce double the frequency using timer interrupt.
Then I would connect the Piezo transducer to two pins, which places the transducer in differential mode (using two pins, which makes the volume much louder than if using a single pin). When a tone required then both pins are toggled in complementary to each other.
When no tone is required, then both pins connected to Piezo transducer, need to stay at 0 or 5V, so there is no PD across piezo transducer.
If you do that every so often you will get a pulsed tone
Normally resonant frequency is between 2 and 5KHz (depending on manufacture)
If you can let me know the resonant frequency, I can help you with a flowchart.
If you Don't know the correct working frequency, then a variable frequency flow chart can be developed.

I would recommend you using a target device with built in PLL so you can have a fast osc speed. e.g above 32MHz

Martin
Martin

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Thanks Martin,

I am unsure about the frequency of the Piezo as it has been pulled out from an old Chinese door alarm.
I have not used timer interrupts before, and the word PLL is new to me, is this used to double the frequency of the main
clock in the PIC?. Would quite appreciate a working example.

regards

indana

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

nmindana wrote:the word PLL is new to me, is this used to double the frequency of the main
clock in the PIC?
Nealy.
PLL (Phase Lock Loop) on a few 18F devices and on newer 16F18xx will allow you run the clock speed of the microcontroller at x4 the internal or external oscillator speed.

E.g You can configure the 16F1824 to run with an internal oscillator set at 32MHz
To do this, you configure the internal oscillator to run at 8MHz. Then in the 'Chip', 'Configure..." menu make sure PLL Enable is set to Enable.

Before I can produce a working example:
Can you let me know what target device, type of osc (internal or external) and the frequency of external crystal/ or resonator if either are used.

Martin
Martin

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Hi Thanks,

I am using 16f88 as I need 18, I/O pins.
Internal osc set at 8Mhz, running flowcode digital clock example.

I think this chip doesn't have a PLL.
May be I should change my target chip to include at least one ADC (one analogue input attached).

16F1824 has only 14pins!. Just Wondered what my alternatives are, without burning my pocket?.

regards

Indana

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

Have a look at the 16F1827. its a powerful chip for the price. That has PLL built in. + 2 CCP chanels so you can control 2 PWM channels etc.

It should work with boostc after the planned Flowcode upgrade.

Meanwhile should work with Hitec compiler. See:
http://www.matrixmultimedia.com/mmforum ... =26&t=6812

I will look at producing a flowchart as soon as I can, and will see if I can base it around your 16F88 device.

Martin
Martin

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Many thanks Martin.

Regards

Indana :D

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

nmindana wrote:Hi Thanks,

I am using 16f88 as I need 18, I/O pins.
No problem.
The 16F88 only has 16 I/O pins. But RA5 is I/P only

Try this. It is untested on real hardware, and is only just therory.
Connect a variable resistor to AN0
Connect the piezo transducer between pins RA1 and RA2
Frequency generated should be variable between Approx 2KHz and 4KHz.(in about 4 ranges) with a osc speed of only 8MHz, if frequency is too high then there will be problems when running things like LCD's etc.
Try adjusting pot so piezo is running at desired volume. (that's if it works :p ). If piezo transducers does not go that loud, then resonant frequency is not being hit.
If you then measure the voltage at AN0, and let me know what it is, I can then calculate the operating frequency.

If you want the tone to be pulse can you let me know the pulse rate you desire.

Once the best operating frequency is determined and duration of tone off/on is posted, pot can be replaced with a switch, and I will alter Flowchart so that only when Enable variable = 1 the piezo transducer will be active.

Martin
Attachments
piezo driver1.fcf
(8.5 KiB) Downloaded 485 times
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

I did not have a chance to test before posting the Flowchart on the above post.
Now I have tested with my hardware, there was a small error within the Flowchart.
Here is an improved version which is tested and does work OK.
Voltage across A1 and A2 = 10V pk to pk
Frequency out (depending on setting of pot):
982Hz
1.2KHz
1.6KHz
2.5KHz
4.9KHz

Martin
Attachments
piezo driver2.fcf
Working Version.
(12.5 KiB) Downloaded 449 times
Martin

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Hi Martin,

You are amazing :D

Works fantastic but I have 2 questions:

1. Presently the tone is continuous, how do I change it to mimic a burglar alarm without reducing the sound level.
2. I am using this as part of a moisture sensing alarm, I use the digital clock example in flowcode for data logging, can I use two TMR0 interrupts in the main programme?. LCD may not function well etc as you mentioned earlier, what are my options?.

Many thanks

Indana

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

Thanks, I'm glad it works.
nmindana wrote: 1. Presently the tone is continuous, how do I change it to mimic a burglar alarm without reducing the sound level.
Done that for you.
nmindana wrote: 2. I am using this as part of a moisture sensing alarm, I use the digital clock example in flowcode for data logging, can I use two TMR0 interrupts in the main programme?
You just use different variables within the timer0 macro. As an example I have used 2 variables. Count is for setting the main frequency. Count2 is for pulsing sound at 1Hz.
nmindana wrote:LCD may not function well etc as you mentioned earlier, what are my options?
It works fine. Interrupt frequency is not too high.

Here is a much improved version. The LCD shows the current set frequency. So if you want to free up port A0, then vary pot, until desired volume. Read Frequency.
If you let me know that frequency, I will alter Flowchart to keep it running at that frequency without pot connected.
Note: With out pot connected, A0 will be floating, so frequency may not be best frequency.
LCD also indicates if alarm is On or Off
If A3=high, then alarm will pulse every second. If A3=low then alarm is disabled.
Piezo Alarm3.jpg
Piezo Alarm3.jpg (19.75 KiB) Viewed 19997 times
Tested on hardware first :P

Martin
Attachments
piezo driver3.fcf
Program was working fine, just displayed frequencies were reversed.
(14.22 KiB) Downloaded 393 times
Last edited by medelec35 on Tue May 24, 2011 11:24 pm, edited 1 time in total.
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

Hi Indana,

If you have tested alarm,is pulsed 1Hz fast enough?
I can change the pulsed frequency to a different one if necessary.
I chose 1Hz because of:
Pulse frequency is determined by the control panel but a frequency of 1 second on, 1 second off, is recommended for compliance with BS 5839 Pt1.
I got this information from a website that sells alarm control panels.

Martin
Martin

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Hi Matin,

It works brilliant. I am trying to understand the programme still, on the face of it looks small, but does quite a
lot. I am trying to use a moisture sensor instead of A3 which will be an analogue input, triggered by an interrupt on pin RA3.

I will buy a proper piezo and will let you know it's frequency. I suppose knowing 'C' will go a long way in making programmes sleeker in Flowcode. I still don't understand some functions within flowcode, and guess somebody than would have to write a bible for novices like me.

Thanks once gain.

Indana

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Hi Martin,

The output although is audible, it isn't enough for my purpose.

I have now procured 1.8KHZ piezo, and will be using 3v battery supply.

Can you modiy the programme to suit my purpose?.

I must say I have tried but failed.

Thanks in advance.

Indana :)

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

Have you had a look that this:
http://www.matrixmultimedia.com/mmforum ... 443#p24776

You can set it to 1.8KHz

What I have found is driving the piezo at resonance, does make a huge difference. If you drive it a bit below or above, it can produce a very loud ear piecing annoying tone :shock:

You could try the Flowcode in the above link just to determine correct best frequency, since it has a much more wider span and higher resolution (100Hz)
Which will determine if 1.8KHz is most suited frequency. if you let me know the frequency from that test, and post the Flowchart you want modifying, I will sort it out for you.

Have you got a link to the data sheet of new piezo transducer?

Martin
Last edited by medelec35 on Mon May 30, 2011 1:13 am, edited 1 time in total.
Martin

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Hi Matin,

Piezo is from Maplin: 27/1.8KHz

http://www.maplin.co.uk/piezo-transduce ... code=YU87U

File attached :)

Thanks

Indana
Attachments
piezo driver final.fcf
(14.78 KiB) Downloaded 325 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

I have modified the Flowchart to allow you to set the sensitivity level. I'm not sure if want alarm to be on if soil goes drier, or wetter. If it's the former then you will need to reverse arrow
From
Level>Set_Moisture
to
Level<Set_Moisture

The other thing is electrolysis eating away the electrodes.
What I would to is use a transformer supply via diodes, cap voltage regulator etc to supply power to microcontroller.
Then I would use the secondary of the transformer to supply LOW AC voltage to soil via a probe.
The other probe that's in the soil connect via Diode/resistor Cap filter to supply a DC voltage that's proportional to the soils resistance.
Then you should not have any electrolysis issues that you would with using DC.

If using A ready made DC power supply then you could use another two ports to produce An AC square wave, just the same as driving piezo transducer. But Not got time to check if can be done with 16F88 due to lack of o/p's (probably not)?
See how you get on with this Flowchart.

Martin
Attachments
piezo driver final modified.fcf
(13.5 KiB) Downloaded 361 times
Martin

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Fantastic :)

Thanks Matin

How can I make the ADC sensor as part of port B interrupt. ie. if an interrupt occurs,main on AN6 (port B) it triggers the macro
"sensor" and the alarm ? I tried but is not working!.

regards

Indana

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Also can enlighten how you worked out the frequency for 1.8Khz.

Thanks Matin

Indana :?

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

nmindana wrote:Also can enlighten how you worked out the frequency for 1.8Khz.

Thanks Matin

Indana :?
Sorry there has been a serious illness in the family which involves back & forwards to hospital.
As soon as I get a chance I will explain.

Martin
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

nmindana wrote:Also can enlighten how you worked out the frequency for 1.8Khz.

Thanks Matin

Indana :?

1) Interrupt frequency required =

Code: Select all

2*required frequency = 2*1800=3600Hz
2) Min prescaller value =

Code: Select all

Fosc/(4*Fint*256) = 8000000/(4*3600*256)=2.17
Since I want to use a tmr0 to be approx 127ish (since i'ts the middle of 0 - 255) I need to round prescaller up. = 4

3) Correct tmr0 value needs to be found. Basically, when timer0 interrupt is when tmr0 register rolls over from 255 to 0.
The quicker tmr0 rolls over then the quicker interrupt macro is accessed again.
So if you place at the beginning of timer0 interrupt macro code a C code block with tmr0=+124; for example, then timer0 will be assigned with 124, hence less time to roll over, causing a higher interrupt frequency.

4) How I determined value of tmr0 to use was to use a C code block containing

Code: Select all

tmr0=+FCV_SET_MOISTURE;
All I need to do is then adjust set moisture, looking at its value on LCD. e.g LCD displays 124
This value is assigned to tmr0, and I know tmr0 is correct value when frequency across A1 and A2 is at correct frequency.
So you alter tmr0=+FCV_SET_MOISTURE; to tmr0=+124;
Hope this helps.

Martin
Martin

nmindana
Posts: 82
Joined: Thu Nov 18, 2010 5:00 pm
Has thanked: 52 times
Been thanked: 5 times
Contact:

Re: Driving Piezo with PIC

Post by nmindana »

Thanks you are brilliant :D .

Wish I had a brain like yours!.

I am sorry to hear about your family crisis.

I wish Speedy recovery

Many thanks for being very supportive.

regards

Indana

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Driving Piezo with PIC

Post by medelec35 »

nmindana wrote:Thanks you are brilliant :D .

Wish I had a brain like yours!.
nah, that will make you very forgetful :p
Besides my brain is not that special. there are Flowcode users far more brainier than myself
nmindana wrote: I am sorry to hear about your family crisis.

I wish Speedy recovery

Many thanks for being very supportive.

regards

Indana
Thank you Indana, that's very kind of you to say :)


Does your piezo transducer sound load enough?
Martin

Post Reply