SevSeg freezes with I2C

Hello, a newbie here. I am trying to make 4-digit 7-segment display to work along with MCP23017, which I am using as I/O expander. The display is directly attached to Arduino Micro, it is common anode type. It shall just to count down an amount of time, regardless of MCP23017 ( this will be used later for buttons and relays).

All works good without MCP23017 , also the times counts down, until i attach SCK/SDA lines to MCP23017. The display freezes in some strange state (always different). Starting the adruino with MCP23017 attached, is worse, the display shows nothing.

MCP23017 has its own 5V from battery, i checked it also without battery.


#include "SevSeg.h"
SevSeg sevseg;

#include <Adafruit_MCP23X17.h>
#include <Wire.h>
Adafruit_MCP23X17 mcp;

unsigned long previousMillis = 0;
const unsigned long interval = 1000;

int TIME = 2000;

int DISP = TIME;

void setup() {

  byte numDigits = 4;                  // SevSeg init
  byte digitPins[] = { 12, 9, 8, 20 };   // SevSeg init
  byte segmentPins[] = {11, 7, 18, 21, 13, 10, 19, 22 }; // SevSeg init
  bool ResistorsOnSegments = false; // SevSeg init
  byte hardwareConfig = COMMON_ANODE; // SevSeg init
  int numPins = sizeof(segmentPins) / sizeof(segmentPins[0]); 
  int numPins1 = sizeof(digitPins) / sizeof(digitPins[0]);

  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, ResistorsOnSegments);  // SevSeg start
  sevseg.setBrightness(90);

  mcp.pinMode(1, INPUT_PULLUP);
  mcp.pinMode(2, OUTPUT);

  sevseg.setChars(DISP);
  Wire.begin(); 
  mcp.begin_I2C(0X27);
}

void loop() {

  sevseg.refreshDisplay();  

 if (TIME > 0) {  
      unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        TIME--;
        DISP = TIME;  
        sevseg.setNumber(DISP);
        
      }
 }

}

I saw some similiar topics at the forum, so for me the conclusion is not good: this library doesn't work, when I2C is on.

Please advise.

Regards

I moved your post to a more suitable location

share a drawing of your circuit with information on power etc

did you add pullup resistors for the I2C line?

(please read How to get the best out of this forum )

1 Like

Aren't they A4 and A5, aka pins 18 and 19?

Welcome to the forum.

The Arduino Micro: https://store.arduino.cc/products/arduino-micro.
The SDA and SCL are at pin 2 and 3.

Let's do this step-by-step.

First the display.
Connect the display and remove everything else from the Arduino board.
Which SevSeg library do you use ?
Can you give a link to your display (a link to where you bought it).
You might need resistors for each segment.

Then the I2C I/O expander
Remove the display from the Arduino board.
Please use the 5V from the Arduino board.
Can you draw a schematic with the wiring ?
Can you show a photo, so I can see if you use a long cable or a flat ribbon cable for the I2C bus.
You may not use the MCP23017 before initializing it. Look at examples from Adafruit how they use the library with "mcp.begin_I2C()": https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/blob/master/examples/mcp23xxx_blink/mcp23xxx_blink.ino
If you fix the sketch, does it work on its own ?

Possible causes
There are many possible cause. The more information you give, the faster we can pinpoint the cause.
At this moment I'm thinking of the bug with "mcp.begin_I2C()", or perhaps a low voltage due to the display, or a shortcut on the I2C bus, or missing power for the MCP23017.

Can you give a reference to that ? It is such a bold claim.

1 Like

Wow, thanks for such a rapid feedback! Thank you, i wasnt expecting that!

I have read all your answers - and it helped. My calim was not correct, but most important is that all works now. Well, not as expected, the display flickers... I am sorry for not providing any sketch, i will try to make it until the end of week, for the future seekers.

First - I added pullup resistors, as J-M-L suggested. I used 4,7 kOhm. I did not see them on examples from internet, so i did not know about them. It helped.

Then i used 5V from Arduino (thx Koeppel) and that all together solved my issues. I also fixed the sketch, as suggested.

I am posting one photo, its full of wires, so you cannot see a lot. The display flickers a lot, the more buttons I am adding, the more delay i have. I am currently switching buttons directly to Arduino, looks like it helps with the issue ( faster response than from MCP23017).

Thank you very much.

That is looking good :+1: The photo is too small and you should not use a yellow wire for the GND, but it is looking good :smiley:

The 7 segment displays are not just turned on and off. Each segment is turned on for a short time. The loop() should run hundreds or thousand times per second to avoid that you can see flickering.

Do you have a multimeter (also called a DMM) ?
Can you check the 5V voltage ? The relays and the display could draw too much current.

I'm looking at the schematic on the Micro page.


Even though T1 is turned on for a reverse current when powered via the USB, the 5V pin of the Micro board should be near 5V.

Thank you very much for your support. I decided to use a standard 16x2 display, with I2C bus. Much more easier for me, without flickering and just two wires.

Thanks one more time!

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