Atmega328 with Grove Buzzer


I have a project with an Arduino Uno, a Grove base shield, a Grove LCD, a grove Buzzer, a servo, and a membrane keypad.
The purpose of this all is to open my door with entering a code instead of using key.
Everything worked fine, but my setup did not look good and was big and messy, so I decided to shrinkify it.
So I taken apart, and started to build it all on a breadboard without the shield and the uno board, just with the atmega328, the crystal and the caps.
I am also redesigning the power supply. Until now i had a usb charger to power arduino and an external battery to power the servo. I decided to use a 12V power supply with a 5V regulator for the microcontroller and a 6V regulator for the servo.
On the breadboard I dont have yet the 6V reg and the servo, so I just use an LED to show me when the servo is attached. (i always detach when not in use to prevent clicking).
Everything works fine, except for the buzzer. The buzzer should beep when a button is pressed, or the door is open, or a wrong code has been entered.
If i plug the buzzer, the system collapses as soon as it gets the first signal.
Sometimes it stays on, sometimes just buzzes for a moment. The lcd backlight changes color, letters disappear from screen, and the servo gets attached.(the LED is lit).
I tried to use different ports, analog, digital, always the same results.
I uploaded the Blink example to check if i may overload the system somehow when the beeper is connected, but instead of turning on and off it beeps continoulsy, so the system collapses also when i connect nothing else.
I enclose the way I connect the things together.
Do you have any idea, why it doesn't work with the standalone microcontroller?
What may I do wrong?
Thanks in advance for any help.

Pinmapping :

Do not connect 5V to AREF, please remove that wire.

Could you make a photo of it ? Because I want to see how the wires are with the crystal and the 22pF. Those three components should be close to the ATmega328P. Now I have to comment about the picture, which is weird. For example the display has the 5V and GND exchanged.

Could you give a link to that buzzer ? Is it only a piezo element, or a buzzer that buzzes with DC 5V ?

There is no decoupling capacitor near the ATmega328P, you have to add one or more 100nF to 5V and GND close to the ATmega328P.

The 100nF should be very close to the 7805.

Where is the servo motor in the picture ? Perhaps the servo motor will run just fine at 5V. A servo motor can require a high peak current, about 0.5A for a common servo motor is normal, but a peak of 1A is also possible. A large capacitor could help. With those peak current you have to know how the ground currents are going.

And last but not least : show us your sketch.


Thanks, I removed the wire between AREF and 5V.
I am enclosing a photo of it.
Yes, the display wires have been accidentally changed on the picture, but in the real version they are connected correctly.
Sorry for the weird picture, it is my first try in Fritzing.
This buzzer is the module from a Grove Starter kit for Arduino.
Here is the link:

Before taking it apart and using with grove shield it worked fine with the analogWrite() and the digitalWrite() also.
I used it with analogWrite() to make a nicer sound. Now I tried it with both functions, with neither works properly.

Is it all right if I add a 100nF right beside the microcontroller between pin 7 and 8?

Unfortunately my servo doesn't operate on 5V.
In the beginning I tried it but it only makes a clicking noise at that voltage level.
Then I checked and found out that it needs a voltage betveen 6 and 7.2V.
It is a HJ S3315D model. Since I haven't found any datasheet on google, I link a seller, where u can see the details scrolling down.

The servo is not at the picture because I don't have it connected now.
I managed to kill it with having it blocked while trying to rotate.
So I am waiting for the post to bring the new one.
That's why I just connected a LED to the servo's output to indicate me when servo should be attached.
I have a 12V, 5A power supply so the current draw of the servo is hopefully won't be a problem.

And two pictures of my breadboard now

And an image of the schematic

Thanks in advance :slight_smile:

And sorry, i could use the [ code ] tag for my sketch because i get an error message that the post exceeds 9000 characters so I enclose the .ino file.

lockOnPCB.ino (15.2 KB)

Thanks for all the information.

A 100nF between pin 7 and 8 is okay. Perhaps also one on the other side. That decoupling capacitor will be the most important component on your breadboard.

I can't see very well how far the ground of the 22pF has to travel to get to the GND pin of the ATmega chip. Could you change that and put the ground of the 22pF directly in the breadboard row of the ATmega GND pin ?

I read elsewhere that the voltage of the servo motor is 4.8V to 7.2V.
The brand is "HJ", but I don't know if they have a website.
I think that mechanically blocking it, should not damage it.

The buzzer is just a piezo disc. You can use the tone() function, but the timer usage might conflict with other libraries, for example the Servo library. The analogWrite() also uses a timer.
Here is an alternative tone library : teckel12 / Arduino toneAC / wiki / Home — Bitbucket
At the bottom is a link to TimerFreeTone which does not use a timer at all.
For safety you need a resistor of 120 ohm (or 150, or maybe 100) between the Arduino output pin and the piezo. I suggest that you add that resistor, since the piezo can behave as a capacitor and that causes high current peaks which causes voltage drops of VCC, and that is why decoupling capacitors are so important.

There is one weird but important thing about the display. The cable to the display is a flat ribbon cable and has SDA and SCL next to each other. That is the worst cable for I2C. Please tear those wires apart from the rest from begin to end. I mean both SDA and SCL should be a single loose wire.

I took a quick glance at the sketch, I love a state machine.

The Servo library uses Timer1
Where does the StopWatch library come from ? From here ?
It seems a handy library, I have not used it yet. It does not use a timer.

That means that timer2 is still available. I think the tone() function uses Timer2. Perhaps you should try the tone() for the buzzer. Please don't use analogWrite, that is so confusing, to use the frequency of the PWM for a tone.

There was nothing in the sketch that got my attention.
A few minor remarks:
The values of the states are often declared in a 'enum'.
I don't know if the lcd library supports the 'F()' macro, you could try it.

Thank you very much,
I added a 100nF cap on both sides of the Arduino and a 150 ohm resistor between the output and the piezo.
This solved the problem.
Since I am an amature musician also, I couldn't stand to play around a little with the piezo, and played some music.
So I decided to add some extra features, for example on correct code entry, play some music, and make some fun with those who enter dummy codes like 1234 :slight_smile:
At the moment I use the tone() function, but i will try the alternative one because if I am right that can handle more buzzers, and I like this buzzer so much, that I decided to add one or two more to my project to play more complicated music :slight_smile:
The buzzer i am using now has three pins(probably because it is a part of a grove kit), but I ordered a couple more from eby which is not on a PCB and has only two pins.
I think it will work the same way, with the tone() function, except that when I connect the buzzer, I wont have to connect any wires directly to 5V but with the resistor to the Arduino output pin.
Here is the link of the buzzers i bought:

About the display...
I like that ribbon cable because it is easy to organize, since it will go under my door.
I read about the shielded cables.
What if I add one ground wire beside both the SDA and SCL lines?
Or is the only safe way to do it tearing them away?
In my setup they will probably be near many other cables.
So how should I do it.

Yes, I have the StopWatch library that you linked.

Thanks for the remarks about the enum, and the F() macro.
At the moment I don't know what F() macro is but i will read it and consider using it.
I heard about the enums, maybe I will change the code later.

I am enclosing a pic about the position of crystal and the 22pF caps.
I could not connect it directly to that row, but it is very close, and works at the moment.
But on the PCB layout I will put them as close as possible.

Thanks for your useful advices :slight_smile:

More buzzers, that will be very disappointing :frowning:

There are buzzers and piezo discs. A buzzer that is buzzing when 5V is applied can not be used with the tone() function.
Did you know that a link to Ebay can be as short as this :
Those are buzzers, you don't want them.

Search on Ebay for: piezo elements. Those are the bare piezo discs. With a case they sound better. You can also use them as knock-sensor or contact-microphone.

Try Mozzi : Mozzi
It completely takes over the microcontroller, but it can make awesome sounds.
The examples on that webpage are the actual sounds that a ATmega328P can make.

About the display...
A ground wire is also not good. As a thumb of rule : a I2C bus can be up 50 cm.
When SDA and SCL are next to each other in a flat ribbon cable, they can cause a voltage spike in the other wire. When one of them is near GND, the capacitance increases and that decreases the possible length of the cable.
Two wires so close to each other in a flat ribbon cable makes a very good (or should I say: "very bad") capacitive/magnetic/electric coupling.

Near other wires is possible, but rather not near wires that have high current or current peaks, for example motors or servo motor.

Do you know what the combined pullup resistor value is for SDA and SCL ?

The 'F()' macro:

Serial.println("Hello");   // the text "Hello" is in flash memory and in sram.
Serial.println(F("Hello"));  // the text "Hello" is only in flash memory.

To see your images, one has to log in at Google. I don't want to do that.
You can attach an image. If you click the "Reply" button and then click "Attachments and other options" then you can attach an image.


Thank you for your help.

Sorry for disappearing for days, but I had to work quite much and did not have too much time to deal with my project.
I have the buzzers and they work with the tone() function.
They make the music what I want.
But I am not completely satisfied with the result when using more at the same time so I ordered a couple of piezo discs, and they sound much better.
I tried out the Tone library. It can generate more sounds at a time.
So I am using 2 of them to make a two tone melody.
I did not put the music playing code to the main sketch yet, so I don't know if it works perfectly in the program or not, but I will do that this weekend.

My sda and scl wires will be maximum of 20cm.
So I will put them further away from each other and will put near to wires, where no high current flows.
And I dont know what the combined resistor value of them. How can I find it out, and why is that important? {Sorry, I don't know...)

I will try the F() macro with the lcd, and we will see if it works or not.

Now, I tried out if everything works fine or not if I power up the circuit through the voltage regulator, not from the arduino board as I did it before at my tests.
But, I experience a strange thing.
When the microcontroller is waiting for input, sometimes just randomly beeps and enters a digit, usually as soon as it changes program state. For example I enter a wrong code, then it outputs that access denied, and when returns to the wait for input mode, it enters a digit...And also when I am pressing a button, sometimes it detects multiple presses instead of only one...

This porblem doesn't occur, when I am powering the circuit from the arduino, but does occur when I am powering from the 12V adapter and the 5V voltage regulator.

I thought that it might be because of an electrical noise, so I tried to add other decoupling capacitors beside the 2 100nF caps on the sides of the Atmega, but same thing happens. I tried the values of 0.1uF and 1uF. {Of course I have decoupling caps right beside the LM7805, as in the schematics).

So I have no idea what may be the cause...

Have you got any?

Thank you very much in advance

I attach the picture you could not open last time.