1. 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.
    Dismiss Notice

same code but different output!

Discussion in 'Arduino' started by whiz115, Dec 27, 2014.

  1. whiz115

    whiz115 Member

    Joined:
    Jan 29, 2007
    Messages:
    742
    Likes:
    2
    Location:
    DotGR
    hi

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

    Code (text):
    /*
    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?
     
  2. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,491
    Likes:
    186
    Location:
    Prague, Czechia (not Chechnya)
    do you have fuses set up the same? What oscillator are you using?
     
  3. whiz115

    whiz115 Member

    Joined:
    Jan 29, 2007
    Messages:
    742
    Likes:
    2
    Location:
    DotGR
    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.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,491
    Likes:
    186
    Location:
    Prague, Czechia (not Chechnya)

    Well, great, but please answer both my questions.
     
  6. whiz115

    whiz115 Member

    Joined:
    Jan 29, 2007
    Messages:
    742
    Likes:
    2
    Location:
    DotGR
    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.
     
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,808
    Likes:
    135
    Location:
    morristown,tn
    They should be the same If you put the same bootloader in the new chip there are more then one bootloader
     
  8. tunedwolf

    tunedwolf Well-Known Member

    Joined:
    Aug 9, 2006
    Messages:
    1,080
    Likes:
    62
    Location:
    UK
    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 :)
     
    • Like Like x 1
  9. whiz115

    whiz115 Member

    Joined:
    Jan 29, 2007
    Messages:
    742
    Likes:
    2
    Location:
    DotGR
    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 http://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?



    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.
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Are they two Ardunio's or are you swapping the chips over..... The other factor is the LED which is connected to pin 9 this may have a bearing on the problem..
     
  11. whiz115

    whiz115 Member

    Joined:
    Jan 29, 2007
    Messages:
    742
    Likes:
    2
    Location:
    DotGR
    i'm swapping the chips..that's why i uploaded the bootloader to the second atmega328 so i can swap them
     
  12. whiz115

    whiz115 Member

    Joined:
    Jan 29, 2007
    Messages:
    742
    Likes:
    2
    Location:
    DotGR
    any ideas? thank you...
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    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.
     
  14. whiz115

    whiz115 Member

    Joined:
    Jan 29, 2007
    Messages:
    742
    Likes:
    2
    Location:
    DotGR
    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?
     
  15. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,715
    Likes:
    37
    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
     
  16. whiz115

    whiz115 Member

    Joined:
    Jan 29, 2007
    Messages:
    742
    Likes:
    2
    Location:
    DotGR
    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.
     
  17. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,808
    Likes:
    135
    Location:
    morristown,tn
    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
     
  18. whiz115

    whiz115 Member

    Joined:
    Jan 29, 2007
    Messages:
    742
    Likes:
    2
    Location:
    DotGR
    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.
     
  19. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    How did you install the bootloader? You must have a programmer... The arduino bootloader firmware comes from the Arduino playground and there are several versions....
     
  20. whiz115

    whiz115 Member

    Joined:
    Jan 29, 2007
    Messages:
    742
    Likes:
    2
    Location:
    DotGR
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    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....
     

Share This Page