Internal oscillator, PB6 or PB7 with piezo buzzer

Hi,

I have a bare bones atmega328 board using the internal oscillator. All the pins are used and i'm wondering whether there is a way to use the PB6 or PB7 pin to control a small piezo buzzer such as this,

http://www.cui.com/product/resource/cmt-1075-smt.pdf

Greatly appreciate any response.

Thanks

Tom202:
Hi,

I have a bare bones atmega328 board using the internal oscillator. All the pins are used and i’m wondering whether there is a way to use the PB6 or PB7 pin to control a small piezo buzzer such as this,

http://www.cui.com/product/resource/cmt-1075-smt.pdf

Greatly appreciate any response.

Thanks

Tom,

Yes: with qualifications, if you are running off the internal RC osc. then the external pins for the crystal can be reused: Consult the datasheet section “Alternate Functions of Port B” for limitations.

To my knowledge, the Arduino pin mapping does NOT provide an alias for these physical pins, so you will have to resort to using port-pin mapping in your code. That said, I have never actually tried it although the pinout for the 328 does show extra functionality. It is far easier (and it works) to use the Reset pin as I/O by use of the fuses - however this is a one-way trip unless you have an HV fuse reset device.

Consult the tech reference manual for the Atmega328 and look over this page: Pin Port examples

Ray

Atmega328P.jpg

Hi Ray,

Thanks for your response.

I managed to get the PB6 and PB7 working as outputs using the following from here: http://joshfire-tech.tumblr.com/post/79859137091/how-to-use-pb6-and-pb7-on-an-atmega328-when-youre

It works well. Does the use of PB6 or PB7 with all the other pins in use affect the overall operation in any way and can problems arise with this setup? Can the piezo still work using the Tone library using PB6 or PB7?

Greatly appreciate any help.

Cheers

Tom202:
<…>
It works well. Does the use of PB6 or PB7 with all the other pins in use affect the overall operation in any way and can problems arise with this setup? Can the piezo still work using the Tone library using PB6 or PB7?

I think it is more difficult since the syntax for tone is:
tone(pin, frequency)
tone(pin, frequency, duration)

And to my knowledge, “pin” for PB6 and PB7 are not mapped in Arduino. Essentially, you have a made-up pin alias as D6 or 6, but those aliases do not exist! So, in the example you pointed to, the code
// set PB6 high
PORTB |= (1 << PORTB6);

is the access methodology. You "could’ improvise and create entries in pins_arduino.h after which you have the aliases defined.

Other than not having a pre-defined alias, the PORT B PB6/7 should act as any other pin on PORT B unless there is something in the 328P technical manual that indicates otherwise.

Ray

Hi Ray,

I decided to ignore the use of the Tone library and instead have the buzzer turn on or off using the same approach as the example.

Cheers

Tom202: Hi Ray,

I decided to ignore the use of the Tone library and instead have the buzzer turn on or off using the same approach as the example.

Cheers

Reasonable. So, just encapsulate the PB6 & PB7 code in a function, bool MyPB6() and bool MyPB7(). Then you can just return true or falkse from the function for input OR if the functions are outputs, create them as void MyPB6( book state) and set state to true or false before calling the function which will set the pin output to state.

Ray