Flickering LEDs Arduino Nano

Hey there

I built a puzzlebox. To measure Time i added a 7-segment display. Also, i added a Neopixel-Ring that shows visualy how much time is left. The goal of the puzzle-box is to find 3 keys to the corresponding key-switches. Each key-switch has its own LED (10 mm LED) which turns on, when the key-switch is turned. To start the Game I added another push button with an LED inside. Also i added a DF-Player to play sound as soon as the game starts.
When i turn on the Arduino (Nano-Clone) everything looks fine and as intended. The "Start-button" is blinking an the 7-Segment-Display shows "Play".
The problem i'm facing is the following:
As soon as I push the "Start-button" one LED (or sometimes multiple) of the "Key-switch"-LED start flickering. Sometimes the 7-Segment display does not work properly (also flickering and not showing the remaining Time correctly).

Could it be, that I did something wrong in my Coding?

This is the code i'm using:


// Module connection pins (Digital Pins)
#define CLK 8
#define DIO 7
#include <FastLED.h>
#define numberOfSeconds(_time_) ((_time_ / 1000)% 60)
#define numberOfMinutes(_time_) (((_time_ / 1000)/ 60) % 60)
#include <TM1637Display.h>
#define NUM_LEDS 12 //Anzahl LEDs
#define DATA_PIN 4

CRGB leds[NUM_LEDS];
int seconds;
int minutes;
const byte startLED = 5;
const byte startButton = 6;
unsigned long startTime;
int startLEDBlink = 500;

const uint8_t OFF[] = {0, 0, 0, 0};
//                      .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01011111, B01101110};
const uint8_t LOST[] = {B00111000, B00111111, B01101101, B01111001};

TM1637Display display(CLK, DIO);

unsigned long timeLimit = 3606000;
unsigned long timeLimitLEDRing = timeLimit;
int Counter = 1;

const byte anzahlSchluessel = 3;
byte schluessel[anzahlSchluessel] = {3, 10, 12};
byte schluesselLED[anzahlSchluessel] = {2, 9, 11};
bool schluesselLastState[anzahlSchluessel] = {1, 1, 1};
bool schluesselCurrentState[anzahlSchluessel] = {1, 1, 1};
byte schluesselAlleSchluessel[anzahlSchluessel] = {0, 0, 0};



#include <Arduino.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>

SoftwareSerial mySoftwareSerial(A0, A1); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);


void setup() {
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true) {
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }
  Serial.println(F("DFPlayer Mini online."));

  myDFPlayer.volume(24);  //Set volume value. From 0 to 30
  pinMode(5, OUTPUT);
  pinMode(6, INPUT_PULLUP);
  display.setBrightness(0x0c);
  display.setSegments(OFF);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);  // GRB ordering is typical
  FastLED.setBrightness(100);
  for (int i = 0; i < NUM_LEDS; i++) {
    if (i < 7) {
      leds[i] = CRGB::Green;

    }    else if (i < 10) {
      leds[i] = CRGB::Yellow;
    }
    else {
      leds[i] = CRGB::Red;
    }
  }
  FastLED.show();
  startTime = millis();
  displayText();
  for (int i = 0; i < anzahlSchluessel; i++) {
    pinMode(schluessel[i], INPUT_PULLUP);
    pinMode(schluesselLED[i], OUTPUT);
  }

}


void countdown() {
  long timeRemaining = timeLimit - millis();
  while (timeRemaining > 0) {
    if (timeRemaining <= ((timeLimitLEDRing - 2000) - (Counter * ((timeLimitLEDRing - 3000) / 12))) && Counter <= 12) {
      leds[Counter - 1] = CRGB::Black;
      Counter++;
      FastLED.show();
    }
    seconds = numberOfSeconds(timeRemaining);
    minutes = numberOfMinutes(timeRemaining);

    display.showNumberDecEx(seconds, 0, true, 2, 2);
    display.showNumberDecEx(minutes, 0x40, true, 2, 0);

    timeRemaining = timeLimit - millis();
    for (int i = 0; i < anzahlSchluessel; i++) {
      schluesselCurrentState[i] = digitalRead(schluessel[i]);
      if (schluesselCurrentState[i] != schluesselLastState[i]) {
        if (schluesselCurrentState[i] == 0) {
          digitalWrite(schluesselLED[i], HIGH);
        }
        else if (schluesselCurrentState[i] == 1) {
          digitalWrite(schluesselLED[i], LOW);
        }
      }
      schluesselLastState[i] = schluesselCurrentState[i];
    }
    if (memcmp(schluesselCurrentState, schluesselAlleSchluessel, 3) == 0) {
      onSolve();
    }
  }
}
void displayText() {
  display.setSegments(PLAY);
  delay(2000);
}

void lost() {
  myDFPlayer.play(3);
  while (1) {
    fill_solid( leds, NUM_LEDS, CRGB::Red);
    FastLED.show();
    display.setSegments(OFF);
    delay(500);
    fill_solid( leds, NUM_LEDS, CRGB::Black);
    display.setSegments(LOST);
    FastLED.show();
    delay(500);
  }
}
void onSolve() {
  myDFPlayer.play(2);
  delay(50);
  myDFPlayer.volume(30);
  while (1) {
    display.setSegments(OFF);
    for (int i = 0; i < anzahlSchluessel; i++) {
      digitalWrite(schluesselLED[i], LOW);
    }
    digitalWrite(startLED, LOW);
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB::Black;
      FastLED.show();
      delay(50);
    }


    for (int i = 0; i < anzahlSchluessel; i++) {
      digitalWrite(schluesselLED[i], HIGH);
    }
    digitalWrite(startLED, HIGH);


    display.showNumberDecEx(seconds, 0, true, 2, 2);
    display.showNumberDecEx(minutes, 0x40, true, 2, 0);

    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB::Green;
      FastLED.show();
      delay(50);
    }
  }
}

void loop() {
  if (digitalRead(startButton) == 1) {
    if (millis() > startTime + startLEDBlink) {
      if (digitalRead(startLED) == HIGH) {
        digitalWrite(startLED, LOW);
        startTime = millis();
      }
      else {
        digitalWrite(startLED, HIGH);
        startTime = millis();
      }
    }
  }
  else if (digitalRead(startButton) == 0) {
    digitalWrite(startLED, LOW);
    timeLimit = timeLimit + millis();
    myDFPlayer.play(1);  //Play the first mp3
    displayText();
    countdown();
    lost();
  }
}

I tried to draw the circuit (DF-Player is connected to A0 and A1):

I tried changing the Arduino. This did not help.

Does someone have an idea why this is not working properly?

Thank you for any advice!

may be too much pressure from your WS2812B?

side note:

  • don't do time compare like this
    if (millis() > startTime + startLEDBlink) {
    always use subtraction to be fine when millis() rollover occurs
    if (millis() - startTime > startLEDBlink) {

  • the delay(2000) probably does not help with reactivity. is it necessary? you should consider a millis() based approach as well.

Can you either add to the existing schematic (nice job) or another showing the power supplies and associated connections. It sounds like you do not have enough power and the supplies are starting to collapse.

1 Like

Thank you for your your answers and ideas.

The 7-segment-Display as well as the LED-Ring are powered through the Arduino Nanos 5V-pin. The Arduino itself gets its power from a 12 V 8 A power supply.
I'm going to test if it works properly when I power the LED-Ring with its own power supply.

I'll keep you updated!

Depending how many leds are on at a given point, the LED ring will draw likely too much from you Nano - esp as the regulator is already pretty busy warming up the planet by wasting energy in transforming 12V into 5V, which limits its ability to provide lots of current on top without heating too much.

for the 7 segments display, it depends also how much it will draw.

➜ worth indeed powering them without going through the Arduino.

Ouch.
Google 'buck step down converter 5V' and buy one.

You sorely misunderstand the use of the "Vin" pin.

Essentially it is useless. :roll_eyes:

Don't even try to use it.

You need 5 V to power the WS2812s, the TM1637Display and the Nano which you power via the "5V" pin (and of course, with all grounds connected together). Either you find an adequately rated 5 V supply or a suitable switchmode "buck" converter to derive 5 V from your 12 V if that was all you had.

The on-board regulator of the Nano will overheat when it is required to provide more than a hundred milliamps or so and - hopefully reversibly - shut down. Before it does that, it may perhaps operate "in limbo" and do intermittent things, perhaps such as flickering.

So if the on-board regulator is not capable of providing more than a hundred milliamps or so, it is of absolutely no consequence whatever that a 12 V power supply could supply eight Amps. :roll_eyes:

Isn’t this regulator a LM1117? Where does this 100mA come from?

100 mA over 7 V = 700 mW. No real heatsink.

Right, that’s under 12v power.

Yeah. You can go a bit higher if you apply something like 7V. But it turns out that 99.8% of people who use the Vin barrel jack apply 12V to it :wink:

Hi everyone

I just wanted to tell xou that i broke my Neopixel-ring when trying to solder the Vin-Pins to another power supply. As i don't have another Ring that fits my project. So I'll have to wait until the new ones arrive. Nontheless I wanted to thank you guys again for your ideas and inputs (Best community ever :smiley: ). If the problem still occurs when I implemented the new ring I'll let you know!

Until then: Have a great time tinkering!

too bad.. have fun too!

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