1

I have an Arduino Nano (running by default at 16Mhz). I am connecting a 20x4 LCD screen via I2C (using A4 for SDA and A5 for SCL).

Here's the very simple code (which was previously only showing black blocks on the screen -- but now seems to be showing odd chars). LCD bad chars

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 20, 4);

void setup() {
 lcd.begin();

  lcd.setCursor(2,0);
  lcd.print("test this");
}

void loop() {
  lcd.setCursor(5,2);
  lcd.print("Hello, world!");
}

ATmega328P DIP Circuit

I've been using this same LCD screen in another circuit which uses a raw ATMega382P (DIP) and it works 100% fine.

I put the LCD back into the circuit with the DIP, tested it and it works fine.

Then I remembered, that ATMega328P DIP runs at 8Mhz.

Run At 8Mhz

So that gave me the idea of running my Nano at 8Mhz just to see if it would work.

I altered the previous program just to set the CPU speed. Here's the entire code again and a snapshot of it working.

#include <LiquidCrystal_I2C.h>
#include <avr/power.h>
LiquidCrystal_I2C lcd(0x3F, 20, 4);

void setup() {
   clock_prescale_set(clock_div_2);  // Sets to 8Mhz
 lcd.begin();

  lcd.setCursor(2,0);
  lcd.print("test this");
}

void loop() {
  lcd.setCursor(5,2);
  lcd.print("Hello, world!");
}

LCD 16Mhz

Questions

I've never changed anything on that LCD. Is there something I'm missing? I thought the SCL line (which is driven by the CPU) set the speed anyways??? -- -- and the LCD was just a slave.

Do you know of any reasons why this would behave this way? Is there something I can change to run it at 16Mhz?

  • 1
    Can you print the value of the macro F_CPU on the serial monitor (or LCD)? Maybe the code (Wire.h) has compile-time checks for the clock speed and adjusts TWBRn wrong? (ww1.microchip.com/downloads/en/DeviceDoc/… chapter 26.5.2) – Maximilian Gerhardt Jun 14 '18 at 23:02
  • As soon as I read your comment it flipped a switch on in my brain and I remembered what I have done. I feel a bit foolish. I altered my boards.txt so it has this line : nano.build.f_cpu=8000000L which is setting it to build for 8Mhz. I was doing that because I use Arduino IDE to build HEX files for my ATmega328P DIP and then I use AVR Dude to flash them to my chip. I'm running ATmega328P DIPs on internal 8Mhz. Thanks very much, I wouldn't have remembered that I had altered that. – raddevus Jun 14 '18 at 23:51
  • 1
    Wow, that was a quite a shot into the dark but I hit something :D. Doesn't the Arduino IDE have specific board variants for 8MHz and 16MHz variants which you could use? Anyways, I guess you can write your own answer to the question and accept it. – Maximilian Gerhardt Jun 15 '18 at 0:00

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.