Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

same code but different output!

Status
Not open for further replies.

whiz115

Member
hi

i was experimenting with an arduino and i uploaded the following example
to two atmega328

Code:
/*
Piezo
This example shows how to run a Piezo Buzzer on pin 9
using the analogWrite() function.
It beeps 3 times fast at startup, waits a second then beeps continuously
at a slower pace
*/

void setup()  {
  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);
  beep(50);
  beep(50);
  beep(50);
  delay(1000);
}

void loop()  {
  beep(200);
}

void beep(unsigned char delayms){
  analogWrite(9, 20);      // Almost any value can be used except 0 and 255
                           // experiment to get the best tone
  delay(delayms);          // wait for a delayms ms
  analogWrite(9, 0);       // 0 turns it off
  delay(delayms);          // wait for a delayms ms 
}

but the outcome is not the same!!! although both atmega328 use the same code
and same setup / buzzer, the sound pitch is different...how can that be possible?
 
i'm using the arduino board and every time i want to program the other atmega328 i'm switching them
on the board, the first atmega328 is the one that came with the arduino board.
 
I don't have to setup anything... i'm just using the arduino IDE and i'm uploading the code.
The crystal is the onboard standard one (16MHz), no fuses or anything else to set.

i forgot to say that the second atmega328 was empty and i uploaded an arduino bootloader
by using the arduino board as ISP, so then i can replace it with the first one and upload
the above buzzer example.
 
They should be the same If you put the same bootloader in the new chip there are more then one bootloader
 
If the beeper is a piezo in a plastic housing, any tension on the housing will alter the pitch of the beeper slightly. To confirm your issue is real, you will really need to scope the output and measure the frequency/ period etc :)
 
They should be the same If you put the same bootloader in the new chip there are more then one bootloader

i think they don't have the same bootloader.. but why is this important? on the empty atmega328 i uploaded the bootloader
as described here on this tutorial https://arduino.cc/en/Tutorial/ArduinoToBreadboard

i didn't had the 16Mhz crystal so i tried the setup described without crystal, now each time i'm trying to upload a code with the
usual arduino settings it gives an error message and i have to choose the "ATmega328 on a breadboard (8 MHz internal clock)"
from he Tools > Board menu, although the atmega328 it's on the arduino board, how can also solve that issue too?



To confirm your issue is real, you will really need to scope the output and
measure the frequency/ period etc :)

for sure the frequency is different one atmega328 measures 490Hz and the other 244Hz.

i've commented some line of code so i get a long beeeeep.
 
If the PWM is fixed at 490hz then the second arduino hasn't been set up with the correct information.. It seems though the base frequency is wrong..... Set up any pin for a square wave ... program both chips and measure both... then you'll know.
 
what frequency the above code should output? can you give me a sample code
of what you want me to do?

could it be the problem the bootloader i uploaded? and how can i correct it in that case?
 
sounds like clock problem i had when i used my own software 232 communication method, in PIC microcontrollers , we setup oscillator in code:

OSCCON= 0b01110000; // MHz please

but when I swapped out chip i had same problems, i was able to realign chips by tweaking this value:
OSCTUNE=0b11011100;
in the end they both had different osctune values to get working,
I wonder if you did not adjust that value if they:
a)had different default OSCTUNE values,
or
B)maybe no 2 clocks are alike, and always need individual tuning,

I wonder if ATmega has OSCTUNE instruction, or what is OSCCAL instruction is?, never used ATmega, so this is just guessing!

Datasheets usually have details on these things
http://www.atmel.com/images/atmel-8...pa-88a-88pa-168a-168pa-328-328p_datasheet.pdf
 
B)maybe no 2 clocks are alike, and always need individual tuning,

if that's the case...to me it looks serious and it shouldn't be like that...
but what's the correct frequency for that "beep"? is it 490Hz?

also is it certain it has nothing to do with the way i uploaded the bootloader?
i'm asking this because it doesn't seem so ok that the second atmega328 cannot
work with the usual arduino settings.
 
If you program the chip with boot loader for without crystal when to load code to it you have to set it uno @ 8mhz if your using the default it loads code for 16mhz.

Your delays will be wrong everthing using time will be based on wrong clock speed
 
If you program the chip with boot loader for without crystal when to load code to it you have to set it uno @ 8mhz if your using the default it loads code for 16mhz.

Your delays will be wrong everthing using time will be based on wrong clock speed

i cannot program that atmega with the usual "arduino uno" setting it gives an error message and i have to choose the
"ATmega328 on a breadboard (8 MHz internal clock)" setting.

the other atmega that came with the arduino board can be programmed just fine wtih the "arduino uno" setting.
 
If you read that page It say's not to use a basic Uno to do this...

If you program the second MEGA328 with the bootlaoder firmware then you only need to download via the serial port.

You know you can buy these chips ready programmed....
 
Status
Not open for further replies.

Latest threads

Back
Top