Adjusting brightness of 1-digit 7-segment indicator with 74HC595

The goal of the project is to display numbers from 0 to 9 using seven segment indicator, 74HC595 and pushing remote control buttons. It is assumed that brightness of the numbers, that are displayed, have to be changed by toggling OE pin to LOW and HIGH. I built the scheme physically. Numbers are displayed correct, but when I press "down" button offTime is changing to 51 (increasing HIGH level pulse length, but it means less brightness cause OE is inverted). After that program gets hung up and Arduino not responds. I assumed further brightness adjustment or starting to display other number in this time. How to fix this problem?

#include <IRremote.h>
int RECV_PIN = 12;
int data = 7;
int OE = 6;
int latch = 9;
int clock = 10;
//int reset = 11;

unsigned long offTime = 200;
boolean changed = false;
int input;

int debounce_delay = 1000;
unsigned long last_press = 0;
boolean flag = false;
boolean result = false;

const int button0 = 82;
const int button1 = 22; 
const int button2 = 25;
const int button3 = 13;
const int button4 = 12;
const int button5 = 24;
const int button6 = 94;
const int button7 = 8;
const int button8 = 28;
const int button9 = 90;
const int down = 21;
const int up = 70;


//1 - dp, 2 - e, 3 - d, 4 - c
//5 - g, 6 - f, 7 - a, 8 - b

void print_0()
{
  //a,b,c,d,e,f = 1    g,dp = 0
  digitalWrite(latch, LOW);
  //digitalWrite(OE, LOW);
  shiftOut(data, clock, LSBFIRST, 0b01110111);
  digitalWrite(latch, HIGH);
}

void print_1()
{
  //b,c = 1    g,dp,a,e,f,d = 0
  digitalWrite(latch, LOW);
  //digitalWrite(OE, LOW);
  shiftOut(data, clock, LSBFIRST, 0b00010001);
  digitalWrite(latch, HIGH);
}

void print_2()
{
  //a,b,g,e,d = 1    f,c,dp = 0
  digitalWrite(latch, LOW);
  //digitalWrite(OE, LOW);
  shiftOut(data, clock, LSBFIRST, 0b01101011);
  digitalWrite(latch, HIGH);
}

void print_3()
{
  //a,b,g,c,d = 1    f,e,dp = 0
  digitalWrite(latch, LOW);
  //digitalWrite(OE, LOW);
  shiftOut(data, clock, LSBFIRST, 0b00111011);
  digitalWrite(latch, HIGH);
}

void print_4()
{
  //f,g,b,c = 1    a,e,d,dp = 0
  digitalWrite(latch, LOW);
  //digitalWrite(OE, LOW);
  shiftOut(data, clock, LSBFIRST, 0b00011101);
  digitalWrite(latch, HIGH);
}

void print_5()
{
  //a,f,g,c,d = 1    e,b,dp = 0
  digitalWrite(latch, LOW);
  //digitalWrite(OE, LOW);
  shiftOut(data, clock, LSBFIRST, 0b00111110);
  digitalWrite(latch, HIGH);
}

void print_6()
{
  //a,f,g,e,d,c = 1    b,dp = 0
  digitalWrite(latch, LOW);
  //digitalWrite(OE, LOW);
  shiftOut(data, clock, LSBFIRST, 0b01111110);
  digitalWrite(latch, HIGH);
}

void print_7()
{
  //a,b,c = 1    f,g,e,d,dp = 0
  digitalWrite(latch, LOW);
  //digitalWrite(OE, LOW);
  shiftOut(data, clock, LSBFIRST, 0b00010011);
  digitalWrite(latch, HIGH);
}

void print_8()
{
  //a,b,c,f,g,e,d = 1    dp = 0
  digitalWrite(latch, LOW);
  //digitalWrite(OE, LOW);
  shiftOut(data, clock, LSBFIRST, 0b01111111);
  digitalWrite(latch, HIGH);
}
//1 - dp, 2 - e, 3 - d, 4 - c
//5 - g, 6 - f, 7 - a, 8 - b
void print_9()
{
  //a,b,c,f,g,d = 1    dp,e = 0
  digitalWrite(latch, LOW);
  //digitalWrite(OE, LOW);
  shiftOut(data, clock, LSBFIRST, 0b00111111);
  digitalWrite(latch, HIGH);
}

void clearDisplay() {
  digitalWrite(latch, LOW);
  shiftOut(data, clock, LSBFIRST, 0b00000000);
  digitalWrite(latch, HIGH);
}

void setup()
{
  Serial.begin(9600);
  pinMode(RECV_PIN, INPUT);
  IrReceiver.begin(RECV_PIN, DISABLE_LED_FEEDBACK);
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);
  //pinMode(reset, OUTPUT);
  //digitalWrite(reset, HIGH);
  pinMode(OE, OUTPUT);
  analogWrite(OE, 0);
  clearDisplay();
}
             
void loop() {

  result = IrReceiver.decode();
  if (result && flag == false && millis() - last_press > debounce_delay) {
    flag = true;
    last_press = millis();
    input = IrReceiver.decodedIRData.command;
    //Serial.print("Received command: ");
    //Serial.println(input);
    switch (input) {
    case button0:
      print_0();
      break;
    case button1:
      print_1();
      break;
    case button2:
      print_2();
      break;
    case button3:
      print_3();
      break;
    case button4:
      print_4();
      break;
    case button5:
      print_5();
      break;
    case button6:
      print_6();
      break;
    case button7:
      print_7();
      break;
    case button8:
      print_8();
      break;
    case button9:
      print_9();
      break;
    case up:
      if (offTime >= 51) {
          offTime -= 51;
        } else {
          offTime = 0;
      }
      changed = true;
      break;
    case down:
      if (offTime <= 204) {
          offTime += 51;
        } else {
          offTime = 255;
      }
      changed = true;
      break;
    }
  }
  if(result == false && flag == true)
  {
    flag = false;
  }
  if (changed) {
    //Serial.print("Updating OE to: ");
    //Serial.println(offTime);
    analogWrite(OE, offTime);
    changed = false;
  }
  IrReceiver.resume();
}

I moved your topic to an appropriate forum category @artur_krush.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Have you tried using an external power supply for the 7seg?

No. Only array of 5V batteries for Arduino board.

I think the eight LED segments of the 7seg are overloading the Arduino 5v pin.

No. Even if a sensible value of series resistor has been chosen, the display would not draw much more than ~140mA.

But @artur_krush has not chosen a sensible value of series resistor. 200K will mean the segments too dim to see even in a very dark room. The current drawn by the display will be ~0.1mA.

@artur_krush is this a real arduino or a simulation?

I am a newbie in electronics. In this case more powerful power supply for Arduino will suit or I should have a power supply (battery) just for 7seg?

Try Post #6 first.

Its screenshot of simulation in TinkerCAD. I have 1 kOhm resistors for scheme with real Arduino.

Try a real arduino, display and breadboard and if the problem also occurs with that, let the forum know on this topic.

You will find that in real life, you cannot push two wires into the same hole on a breadboard!

In real life I built scheme physically with a bit different wiring, but with the same essence. Sometimes I have situation that not all the segments are displayed, when it was suggested. This is probably caused by a weak current in scheme, or lack of Arduino's 5V. But the main problem that I can't regulate the brightness of numbers by PWM to OE pin.

What do you see on Serial Monitor when the Arduino "gets hung up"?

(Do not post screen images. Copy and paste the text from serial monitor and paste into your next post using code tags.)

I receive "Updating OE to: 51" after pressing "down" button. Than at that time "Received command: 0" and after that Arduino don't respond.

brown-black-orange is 20k, not 200k - but still-

As pictured the resistors on '595 pin 2, 3 and those in the same orientation 'on' the 7-segment don't accomplish anything (assuming those pins haven't been lifted and the resistors soldered there).

image

Oh, correct, sorry. But still 100x too high.

It's TinkerCAD.

And real breadboards don't prevent you from inserting resistors in ways that don't accomplish anything either.

They do prevent you from inserting 2 wires into the same hole. Unless you are brutal with them. I've never understood why Fritzing and TinkerCAD allow it.

What if you remove that part?

Run this small code and you will see that only with high OE values ​​(PWM > 200) will you notice changes in the display brightness.

I set up a display here for the test.

int data = 7;
int OE = 6;
int latch = 9;
int clock = 10;
//int reset = 11;

//1 - dp, 2 - e, 3 - d, 4 - c
//5 - g, 6 - f, 7 - a, 8 - b
//----------------------------------------------------------------------
void clearDisplay() {
  digitalWrite(latch, LOW);
  shiftOut(data, clock, LSBFIRST,0b01110111);
  digitalWrite(latch, HIGH);
}
//----------------------------------------------------------------------
void print_0() {
  //a,b,c,d,e,f = 1    g,dp = 0
  digitalWrite(latch, LOW);
  shiftOut(data, clock, LSBFIRST, 0b01110111);
  digitalWrite(latch, HIGH);
}
//----------------------------------------------------------------------
void setup() {
  Serial.begin(115200);
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);
  pinMode(OE, OUTPUT);
  analogWrite(OE, 0);
  clearDisplay();
  print_0();
}
//----------------------------------------------------------------------
void loop() {
  for (int i = 0; i < 256; i += 10){
    analogWrite(OE, i);
    delay(1000);
    Serial.println(i);
  }
}

I updated the code. Changed the debouncing approach and analogWrite parameter to range 200-255. Nevertheless, the new problem occured.
When I choose some command, for example print_0(), IR-receiver gets a command, I have its number on Serial, but when I press other button previous command repeats two or three times. Although I'm calling another command.
The problem of the program loop after decreasing brightness remains, after this command Arduino doesn't respond. Fortunately, brightness really decreases.
I can't get why program works incorrect.

Oh, big difference -- Not.
Can you tell what the OP is doing by looking at that?

@runaway_pancake It's physical variant of scheme. I didn't put resistor legs and wires in one hole in breadboard. But I don't know is it correct to put wire between two legs of resistors.




There are connection, front and back sides of scheme.