Sketch works on Uno, but not on unknown ATmega328 variant

Hey guys and ladies. Uhm....I know all the smart people hang out here. Can someone answer a question. I wrote a sketch and it is on my arduino uno. which has a 328p-pu chip. It works. I have some left over 328's but can't read the lettering on top and don't know if they are 328-328P-or 328P-PU. Is there any reason that either of the three won't work with my sketch? I think they are 328's, I used them in other projects. But might there be something different about them, cause it ain't working. Thanks Ya'll

WELL apparently it has something to do with picco power. After more research it appears there is a difference. In that the "P" is a lower power consumption, which might explain why it isn't working as I thought it would. As I am using POWER_DOWN _MODE in my project. A confirmation would be appreciated. Thanks, Fred

Please don't hijack threads @Fredric58. I have split your post out to its own topic.

In the future, please don't add completely irrelevant replies to a topic. Instead, create your own topic for discussion of distinct subject matters.

This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

Thank you, I READ IT, unfortunately I have what is called "chemo brain", yes it is real. I will try to adhere more closely to topic rules and recommendations. Thank you for your support. Now, is there actually the possibility that an OLD 328 would not be able to handle the low power library that the 328P-PU is possible of assimilating with? My research leads me to believe that, that might be a possibility?

1 Like

In the DIP package, there are 2 different 328 processors. The 328P and the 328 (with no P). To bootload and ICSP program the non-P chips use the MiniCore core. Installation instructions are on the linked page.

Hi, I did that and got "expected a 328P" error. My uno uses a 328P-PU chip. And it works, the others I have are unknown. They are old, used in former projects. They could be 328. (no P). I have checked and 100X checked my wiring and it doesn't perform like when I use the UNO. Thanks. Fred

1 Like

Did what?

Used the minicore, but when I tried to boot load it said "expected a 328P" So I am left wondering if an OLD 328, wouldn't handle the same instructions as a 328P-PU.

Apparently, according to my reading the P-PU is designed for LOW POWER applications. Of course I could be totally wrong......

In the MiniCore setup menu did you choose the 328 processor?

image

1 Like

No I did not. If it matters? it was already boot loaded as I have used it for other projects. I can't remember if it came boot loaded or I or someone else did it for me. It will upload the code without issues. It just doesn't run as expected. In fact it doesn't run at all. I have checked and checked and checked all the wiring, pins and resistors (values) GNDS. I did find 2 bad solder joints but corrected those. Even uploaded a blinky sketch while on the bread board and it worked. Just frustrating after all the hours spent putting it together. Though it isn't the first time my first try at a new project didn't work. These 328's I have came from 3 projects (all the same) I built the boards and had someone else program it. Is there a possibility that he may have boot loaded it in a manner that might not be compatible with my current program even though it will load without issues? Both projects are bare bones with a 16MHz crystal and caps. Thanks

Do you think it might help if you posted your sketch ...

I believe that Nick Gammon's bare bones and Arduino as ISP programmer projects read and report the processor ID of an ATmega-type processor. That will identify it.

Or use any AVR ISP programmer to do so.

Hello Bob, The sketch works fine when run from the UNO. The 328P-PU. Everything performs as instructed. This is while it is on the bread board. If you believe it would be helpful, I can post it.

All I have to work with is my UNO's. I'll take a look at Gammons link.

There is no reason that a sketch written for 328P wouldn't work on a 328, unless you are using software to disable the brownout detect circuit (maybe some low-power libraries?) That seems to be the only difference.

Please post your sketch as it can do no harm to see what you are doing

When you change the physical chip how are you doing it ? Are you taking the original chip out of the Uno and replacing it with the other one or are you using 2 different sets of hardware ?

Yep, that might be the issue I previously mentioned as the P-PU is designed from my understanding to be for low power and perhaps the plain jane 328 can't handle it. Something to do with Picco ??? I can't remember at the moment. However, for some reason I now get an ERROR about the WDT. Not defined. When I compile which is new. I'll post it in a minute, maybe someone can see the problem.

Using the UNO as the programmer with the clone on a bread board with crystal and caps as most tutorials explain.

Now I have an ERROR on compile which just happened. AAAARRRGGGHHH!!!!!!




#include <avr/sleep.h>    // Sleep Modes
#include <avr/power.h>    // Power management
#include <avr/wdt.h>      // Watchdog timer
#include <Servo.h>

struct phaseDataLayout {
  byte angle; // the angle that the servo will turn to
  unsigned long waitingTime; // the waiting time in milliseconds to remain at that angle
};

const phaseDataLayout phaseData[] = {
  {10, 50},   {0, 50},    {10, 50},   {0, 50},    {10, 50},
  {0, 50},    {10, 50},   {0, 50},    {10, 50},   {0, 50},
  {10, 1000}, {0, 1000},  {10, 2000}, {20, 1000}, {30, 250},
  {40, 250},  {30, 100},  {40, 250},  {30, 1000}, {20, 3000},
  {10, 50}, {0, 10},
};

const size_t phaseCount = sizeof phaseData / sizeof * phaseData; //calculate the number of phases in the array

const byte servoPin = 9;
const byte powerPirPin = 12;
const byte pirSignalPin = 10;
const byte powerMosfetPin1 = 11;
const byte powerMosfetPin2 = 13;
enum {POWER_OFF = LOW, POWER_ON = HIGH}; // for ease of reading code
const byte LDR_ENABLE = 8;
const byte LDR_READ = A0;
const int LIGHT_THRESHOLD = 200;  //DO Stuff when darker than this
Servo servo;


// when ADC completed, take an interrupt
EMPTY_INTERRUPT (ADC_vect);

// Take an ADC reading in sleep mode (ADC)
float getReading (byte port)
{
  power_adc_enable() ;
  ADCSRA = bit (ADEN) | bit (ADIF);  // enable ADC, turn off any pending interrupt

  // set a2d prescale factor to 128
  // 8 MHz / 128 = 62.5 KHz, inside the desired 50-200 KHz range.

  ADCSRA |= bit (ADPS0) | bit (ADPS1) | bit (ADPS2);

  if (port >= A0)
    port -= A0;

#if defined(__AVR_ATmega328P__)

  ADMUX = bit (REFS0) | (port & 0x07); // AVcc
#endif

  noInterrupts ();
  set_sleep_mode (SLEEP_MODE_ADC);    // sleep during sample
  sleep_enable();

  // start the conversion
  ADCSRA |= bit (ADSC) | bit (ADIE);
  interrupts ();
  sleep_cpu ();
  sleep_disable ();

  // reading should be done, but better make sure
  // maybe the timer interrupt fired

  // ADSC is cleared when the conversion finishes
  while (bit_is_set (ADCSRA, ADSC))
  { }

  byte low  = ADCL;
  byte high = ADCH;

  ADCSRA = 0;  // disable ADC
  power_adc_disable();

  return (high << 8) | low;

}  // end of getReading

// watchdog interrupt
ISR (WDT_vect)
{
  wdt_disable();  // disable watchdog
}  // end of WDT_vect

#if defined(__AVR_ATmega328P__)
#define watchdogRegister WDTCSR
//#else
// #define watchdogRegister WDTCSR
#endif

void setup ()
{

  pinMode (LDR_ENABLE, OUTPUT);
  pinMode (powerPirPin, OUTPUT);
  pinMode(pirSignalPin, INPUT);
  pinMode(powerMosfetPin1, OUTPUT);
  pinMode(powerMosfetPin2, OUTPUT);
  servo.attach(servoPin);
  digitalWrite (powerPirPin, POWER_ON);
  wdt_reset();
  ADCSRA = 0;            // turn off ADC
  power_all_disable ();  // power off ADC, Timer 0 and 1, serial interface
}  // end of setup

void loop ()
{
  // power up the LDR, take a reading
  digitalWrite (LDR_ENABLE, HIGH);

  int value = getReading (LDR_READ);
  // power off the LDR
  digitalWrite (LDR_ENABLE, LOW);

  // Do Stuff
  if (value < LIGHT_THRESHOLD)
  {
    power_timer0_enable ();
    power_timer1_enable ();
    delay (1);  // let timers reach a known point

    digitalWrite (powerPirPin, POWER_ON); 
  

    if (digitalRead(pirSignalPin) == HIGH){
      // power the servos
      digitalWrite (powerMosfetPin1, POWER_ON);
      digitalWrite (powerMosfetPin2, POWER_ON);

      delay (50);
      

      // perform the defined steps
      for (size_t i = 0; i < phaseCount; i++) {
        servo.write (phaseData[i].angle);
        delay(phaseData[i].waitingTime);

      }
        // turn power off
        digitalWrite (powerMosfetPin1, POWER_OFF);
        digitalWrite (powerMosfetPin2, POWER_OFF);
      }
      
       
    
    
    power_timer0_disable ();
    power_timer1_disable ();
  }

  goToSleep ();
}  // end of loop

void goToSleep ()
{
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  noInterrupts ();       // timed sequence coming up

  // pat the dog
  wdt_reset();

  // clear various "reset" flags
  MCUSR = 0;
  // allow changes, disable reset, clear existing interrupt
  watchdogRegister = bit (WDCE) | bit (WDE) | bit (WDIF);
  // set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
  watchdogRegister = bit (WDIE) | bit (WDP2) | bit (WDP1) | bit (WDP0);    // set WDIE, and 2 seconds delay

  sleep_enable ();       // ready to sleep
  interrupts ();         // interrupts are required now
  sleep_cpu ();          // sleep
  sleep_disable ();      // precaution
}  // end of goToSleep

And now the ERROR is gone.................ANd then it's back!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.