Nano hangs with I2C Library and analogRead

Hi Everyone.

  1. I use a simple Lcd write program with I2C Library and it worked for over 10 minutes.

3.Blink runs for over a hour , same PCB.

3.When I do an analogRead the Nano (Clasic Old Bootloader) hangs up after a few seconds , could vary from 10 sec to 1 minute.
Tested on the same PCB.

4.Default reference(5v) , Internal and External analogRead all hangs up.

  1. Even when the I2C Lcd is removed.

  2. I use Heart Beat led pin 13 and the Led stops Blinking.

My code that hangs.
Thanks in advance for any ideas.


#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

const int LCD_COLS = 16;
const int LCD_ROWS = 2;
float volts = 0;
int value = 0;

void setup()
{
  Serial.begin(9600);
  int status;

  //	begin() will automatically turn on the backlight
  status = lcd.begin(LCD_COLS, LCD_ROWS);
  if (status) // non zero status means it was unsuccesful
  {
    // hd44780 has a fatalError() routine that blinks an led if possible
    // begin() failed so blink error code using the onboard LED if possible
    hd44780::fatalError(status); // does not return
  }
  // initalization was successful, the backlight should be on now

  pinMode(13, OUTPUT);
  //analogReference(EXTERNAL);
  analogReference(INTERNAL);
}

void loop()
{

  value = analogRead(A0);

  //volts = ((5.0 / 1024.0) * value) * 21.29; //21 = devider 10k+10k/1k
  //volts = ((3.286 / 1024.0) * value) * 21.29; //21 = devider 10k+10k/1k
  volts = ((1.04 / 1024.0) * value) * 20.8; //21 = devider 10k+10k/1k

  lcd.setCursor(0, 0);
  lcd.print("      ");

  lcd.setCursor(0, 0);
  lcd.print(volts);



  //--- Heart Beat -----
  digitalWrite(13, HIGH);
  delay(250);
  digitalWrite(13, LOW);
  delay(250);
}

Hi, @mikedb

Can you please post a schematic of your project?

What are you using as a power supply?

What have you got the A0, analog input connected to?

Tom.... :smiley: :+1: :coffee: :australia:

The relay does nothing , part of a previous project.

Your circuit seems to contain no ceramic 0.1uF capacitors. These are needed.
see:- Decoupling tutorial

Hi, @mikedb


You at least need 0.1uF capacitors in parallel with the 100uF.

If you look at the datasheet for the LM7805 it will show you what value caps you should at least have on it's input and output pins.

What is your power supply?
Do you have a DMM? Digital MultiMeter?

Tom.... :smiley: :+1: :coffee: :australia:

@TomGeorge @Grumpy_Mike

I understand , but there is a 100nF close to the Nano across the 5V supply.
I will add caps on the regulator.

Now the #$%&* Nano does not hang up any more, been running for 40 minutes now.

The power supply is mentioned on the diagram 0 to 30V DC 3Amp adjustable bench supply.

Yes I have a few DMM'S.

It did hang up at work with a different Bench supply but now at my house seems fine.

Datasheet does not say much.

Did you Guys ever saw a 0.22uF Electrolytic cap?

Those are not electrolytic capacitors in post #6.

Yes I know.

Vin needs 6volt minimum for it's onboard 5volt regulator.
Connect the 5volt output of the 7805 to the 5volt pin of the Nano, not to the Vin pin.
Check if the 7805 gets hot (and shuts down) 30>5volt is a big drop for an analogue regulator.
Better to use a buck converter instead,
Leo..

Mouser..


Tom.... :smiley: :+1: :coffee: :australia:

Note that there are many data sheets for the LM78xx and the LM340x, each one is subtlety different. This is not an identically clone from all manufactures. Therefore it is vital that you get the data sheet from the same manufacturer as the chip you are actually using.

When in industry I had a case where the purchasing department could not get the exact manufacturers part, but had a replacement that he was assured was identical. Unfortunately it was not, and it oscillated under certain conditions. This caused us to have to scrap about 75 set top boxes because they all failed the factory tests. It should have been tested before it was accepted, but it was not.

Note there are other capacitors you can get at 0.22uF that are not polarised. Unless you use a ceramic capacitor at this value it will be ineffective as a decoupling capacitor due to the frequency response of the capacitor.

The 5V from the regulator is connected to the 5V Pin.
The regulator does get hot with that very bright lcd back light , but not
too hot to touch , when the Nano hangs the voltage is still 5.02V.

Yes that's very true , I orderd IRLF2505 mosfets , datasheet gives RDs on at 2.5V , 3.3V and 5V at a very low resistance.

Ha.. they do nothing with 3V on the gate only switch on above 4.9V.

I do have a lot of these , Polyester caps , will they work for de coupling?

Back to the Nano that hangs , tested now for 45minutes back at work and still working.
I did not fit de coupling caps , just want to see if it will hang up again.

No, not in any way.

When I just left school in the late 1960's we (the development lab where I was an apprentice) learned this lesson the hard way. Things are more widely understood these days.

Testing one product for 45 minutes will tell you nothing useful, when things are on the edge. You need to test at least 100 products at elevated temperature for six months. Then do the maths correctly to calculate the effect on this. It would also require going to a test house where they can generate different levels of interference to find out the susceptibility to RFI for any particular configuration of decoupling capacitor.

There are regulatory requirement for this, for different countries to be complaint with that countries requirements.

This project is just for me testing.

I see that the Nano does not have decoupling caps or a cap on the input of the regulator.

https://www.arduino.cc/en/uploads/Main/Arduino_Nano-Rev3.2-SCH.pdf

If you read that it says:-

So it is not the Official Arduino design.

Missing pullup resistors (say 4.7k) on the I2C lines may cause hanging. The I2C backpack on the LCD should have these but, if you disconnect it, then the pullups are out of circuit.

1 Like

My guess is that the Wire library has gotten confused and has locked up in a loop waiting for an i2c event that will never happen due to some issue on the i2c bus.
Depending on which version of the Wire library you have there is dramatically different behavior as to how the Wire library handles certain i2c exceptions / errors but some end up with the Wire library hanging.

It might be useful to check the return values from the lcd.print() calls.
There are some I2C failures in the Wire library that return status codes, which will be returned through the hd4480 library back to the sketch.
Some of those are effectively fatal. i.e. if you do a subsequent call to Wire to attempt to write something the Wire library will hang.

i.e. if the return value from lcd.print() call is non zero, the Wire library has failed to write the data and depending on the exception code returned, a subsequent call could hang the Wire library.

unfortunately there are certain i2c exceptions that, IMO, are still not handled properly by the Wire library, in that they can cause the Wire library to hang in a tight loop even in the newer wire library that avoids some of the lockup states.

IMO, the Wire library should never hang.

Most of these lockups occur when there is some kind of noise glitch on the bus, often caused by a poor power supply or improper pullups.

The only way to recover from this is to reinitialize everything.

You could run the included I2CexpDIag sketch to see if detects any sort of issue.

--- bill

oops.
I just reviewed thehd44780 code (It's been I while so I have forgotten a bit)
Due to the way the Print class works you won't get very useful status codes.
lcd.print() will just return the number of characters written.
0 will mean no characters characters were written.
But you can get a non zero value and still have an issue.
i.e. a non zero value that is less than the total, but since the total is often not known, like when printing numbers, the return value is not always that useful.

However, lcd.setCursor() will return a status value where zero is ok, and non-zero is an hd44780 library error/exception code. (not a Wire library status)

--- bill

Could this be an option to set explicitly?

https://docs.arduino.cc/language-reference/en/functions/communication/wire/setWireTimeout/

Thanks for the detailed explanation.

How can I see what the version of wire.h is.

So far the Nano did not hang up running for hours.

I used two laptops one at home and one at work.
When I started with this I2C Library it was at home and I don't remember any hanging up.

I reprogramed the Nano at work and fiddled with the Calibration then it start to hang up.

So to reproduce the fault I will program the exact code with the Laptop at work tomorrow and see if the Nano hangs again.

Edit.
Home pc is win10 ide 1.8.19
Work pc is win11 ide 1.8.19