Weird flickering when PWM-controlling LCD-Backlight on standalone ATmega 328

For my current project I'm using a 20x4 Adafruit LCD (Standard LCD 20x4 + extras [white on blue] : ID 198 : $17.95 : Adafruit Industries, Unique & fun DIY electronics and kits). I want to control the Backlight via PWM for fading effects. If I do so on the Genuino micro, everything works finde. If I port it onto a standalone ATmega 328 (with 16Mhz clock), I only get weird flickering.

I thought that maybe the ATmega could be broken but repeated experiments with multiple chips didn't work out. Note however that if I connect a normal LED (with resistor of course), this LED works fine! If I also connect the LCD on the same pin, the weird flickering also happens on the LED.

I'm out of ideas to localize the problem: On the Genuino it works fine, which suggests it's the ATmegas fault, without the display and an LED it works also fine on the plain ATmega, which suggest it's the Display's fault.

For testing purposes I was only using the Fade-Example that ships with the IDE.

Do you have any ideas in how to narrow down the source of this weird behavior or even have an idea about it? Any help is greatly appreciated!

Since you haven't provided any information at all about your standalone system a few questions come to mind. Here are some starters:

  • What are you using for a power supply on your standalone system?
  • Have you adequately bypassed all of the power pins on your standalone system?

Don

Hi.

You tried it with a Micro (32U4), and ported it to a 328.
So you did change the code to work correctly on the respective chip.
Did you also take the pins that offer PWM in consideration, which is likely to be different ?
Also, did you check what current is drawn by the display and take measures for the current to not exceed the maximum allowed current per pin and / or total current for the device ?

You might need to use a transistor to handle the needed current, and the typical setup of that requires the dimmer output of the chip to be inverted (you can easily do that by software).

Thanks a lot for your responses.

floresta:
Since you haven't provided any information at all about your standalone system a few questions come to mind.
[...]
What are you using for a power supply on your standalone system?

Sorry that I didn't make it clear. With standalone I mean a bear Atmega 328 on a Breadboard as described on the Wiki. I'm using it with the external 16 MHz-clock which seemed irrelevant to mention for me. Sorry about that.

To power the whole thing I'm getting power directly out of the USBasp I'm using to program the device.

floresta:
Have you adequately bypassed all of the power pins on your standalone system?

I'm sorry, I'm not sure what you mean by that. If you'd wanted to ask if I connected all GND-Pins on the Atmega to GND and all VCC-Pins to VCC then I'd say: yes, I did.

MAS3:
You tried it with a Micro (32U4), and ported it to a 328.
So you did change the code to work correctly on the respective chip.
Did you also take the pins that offer PWM in consideration, which is likely to be different ?

I'm not sure what changes you mean by that else than changing the pin. I used Pin 9, which is a PWM on both chips.

MAS3:
Also, did you check what current is drawn by the display and take measures for the current to not exceed the maximum allowed current per pin and / or total current for the device ?

You might need to use a transistor to handle the needed current, and the typical setup of that requires the dimmer output of the chip to be inverted (you can easily do that by software).

I already tried a transistor with a 2.2k-resistor on the base (and directly powering the Backlight, not inverted yet). But that had the same effect.

While writing this I think I narrowed down the issue. When powering everything via a Genuino Micro (5V- and GND-Pin via the same USB-Port) or a Duemilanove Board (without Atmega) everything works fine. Only while powering it over the USBasp (also when everything else -- i.e. MOSI, MISO, SCK, RST -- is disconnected) the flickering happens. But as soon as I connect my contraption with the unpowered Arduino Duemlinanove, everything plays nicely (i.e. power from the ISP, but GND and VCC lines connected to a Duemlianove board without USB-connection and processor). What is there on the board that helps in this case? Unfortunately my understanding of the USBasp isn't as deep to understand this.

For reference: I bought my ISP from a no-name-trader. After research I found that it must be this programmer. It has a maximum current of 500mA which shouldn't be taken by the LCD as I can drive it with a normal digital pin on the Genuino Micro (max. of 20mA) or the Atmega (without using PWM).

I'm really thankful for any further ideas on what's the issue.

I am horrified by Wikis that do not show any 100nF capacitors or even mention the subject.

You are relying on the 100nF capacitors present on the Duemilanove or Usbasp. You should really have specific capacitors close to the VCC pins of your Mega328.

Both the Duemilanove and the Usbasp should be able to supply enough current to a PWM backlight.
You still need a resistor in series with the backlight.

David.

I am horrified by Wikis that do not show any 100nF capacitors or even mention the subject.

The Wiki that he followed is one of those that I alluded to in this recent post I made in another thread.

You may not be old enough to remember the Black Flag 'Roach Motel' commercials from the early 1980s. The tag line was "Roaches check in, but they don't check out!".

Well, the Arduino playground is something like that. It seems to be relatively easy to post something to the playground but it seems to be impossible to get anything removed. This not only includes obsolete information . . . but also incorrect or incomplete information.

Don

It is a feature of Internet Tutorials in general.

Great effort is put into clear diagrams.
100nF capacitors and LED series resistors are often omitted in the process.

If someone has gone to the effort of making a good Fritzing diagram it is a pity that they are not complete. After all, users should be able to rely on making an exact copy. Preferably matching the wire colours too.

David.