7 segment display isnt working right

i have a project where i need to display a nummber but its just not working.
it should display 2222 but its just not and i cant find a reason.

#include <SoftwareSerial.h>
#include<Wire.h>
#include <MPU6050.h>
#include "SevSeg.h"

SevSeg sevseg; 

SoftwareSerial mySerial(A2, -1); // RX = A4, TX not needed

MPU6050 mpu;

void setup() {
    byte numDigits = 4;
    byte digitPins[] = {10, 11, 12, 13};
    byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};

    bool resistorsOnSegments = true; 
    bool updateWithDelaysIn = true;
    byte hardwareConfig = COMMON_CATHODE; 
    sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
    sevseg.setBrightness(50);
    mpu.initialize();

    Serial.begin(115200);  // USB Serial Monitor
    mySerial.begin(9600);  // UART for ESP32
    if (!mpu.testConnection()) {
        Serial.println("MPU6050 connection failed!");
        while (1);
    }
    
    Serial.println("MPU6050 connected.");
}

void loop() {

    if (mySerial.available()) {
        sevseg.setNumber(2222);
        sevseg.refreshDisplay(); 
        String rawDataStr = mySerial.readString(); // Read full message as string
        int rawData = rawDataStr.toInt(); // Convert String to int
        Serial.print("Raw Data: ");
        Serial.println(rawData); // Print exactly what was received 
        while(1 == 1) {

          int16_t ax, ay, az;
          mpu.getAcceleration(&ax, &ay, &az);
      
          // Convert raw data to G-force
          float accelX = ax / 16384.0;
          float accelY = ay / 16384.0;
          float accelZ = az / 16384.0;
      
          // Compute total acceleration magnitude
          float totalAccel = sqrt(accelX * accelX + accelY * accelY + accelZ * accelZ);
      
          // Detect shaking: sudden acceleration changes
          if (totalAccel > 1.5) { // Adjust threshold as needed
              int endnum = random(1, rawData + 1); // Ensure rawData is set before use
              Serial.print("Random number: ");
              Serial.println(endnum);

              sevseg.setNumber(2222);
              sevseg.refreshDisplay(); 

              break;
          }
      
          delay(100); // Adjust for responsiveness
  
        }
        Serial.print("End");
    }
}

Hi, you posted your code, good!

Now when I look at your photo.... spaghetti is the first word I can think of. And no it's not any critic of your wiring, but to me it makes no sense at all. Why don't you make a diagram och schematics instead, that will be of much more help to us.

Have a great day!

Hi, @pinguin_hd

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Can you post a version of your code with just the display code?
Did you write your code in stages?
If so you should have a simple display code.

What model Arduino are you using?

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

Hey yeah i know i didnt add any schematics because the wire ring works if i up load this code:



#include "SevSeg.h"
SevSeg sevseg; 

void setup(){
  byte numDigits = 4;
  byte digitPins[] = {10, 11, 12, 13};
  byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};

  bool resistorsOnSegments = true; 
  bool updateWithDelaysIn = true;
  byte hardwareConfig = COMMON_CATHODE; 
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  sevseg.setBrightness(50);
}

void loop(){
    sevseg.setNumber(2222);
    sevseg.refreshDisplay(); 
}

it works thats why i am wondering and yes its the same as in the code that i added befor. I also found that i i put the sevseg.setNumber(2222); sevseg.refreshDisplay(); at the first thing at the loop it works. i will make a schematic now tho

If it works, then it sounds like the code is the culprit.

i am using a Elegoo Uno R3

The image is missing the RX cable for the ESP because i cant add that.

You need to have sevseg.refreshDisplay() at the end of loop() outside of the if(mySerial.available()), or the display will never refresh.

3 Likes

What you haven't realised yet is that the display is multiplexed, but doesn't have it's own multiplexing driver chip, so the Arduino is performing the multiplexing. Specifically the SevSeg library is performing the multiplexing. In order to do that, it needs to refresh the display at least a couple of hundred times every second. But in your code, it only ever gets to do that when some serial characters are available to be read. Most of the time, there aren't any.

So as mentioned already, you need to move that sevseg.refreshDisplay() out of all the if statements. But you also need to remove that delay(100) because that will limit the SevSeg library to at most 10 updates per second, which will flicker like crazy.

EDIT: Sorry, you don't need to remove the delay(100) because that only happens when serial characters are available.

1 Like

You are damaging your Arduino!

This line from the code you posted should give you a clue what you need to do.

Thank you all for you help. Adding the sevseg.refreshDisplay(); outside of the if worked! :3

Now you have to add resistors to the segment wires as alluded to by @PaulRB

yeah i did its just a temp setup i am still thinking about a desigine. But thanky you :folded_hands:

Have a nice day!

1 Like

So just a simulation at this point? Ok... Looked from your post #1 that damage could already have been done.

Arduino simulators are very popular these days, but unfortunately they don't teach beginners about mistakes that would damage real components.

Do you know how to calculate what value of resistors you should use? The 220 or 330 ohm resistors commonly used with individual LEDs won't be high enough.

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