# Issue coding RTC 7 segment clock

**URL:** <https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233>\
**Category:** Programming\
**Created:** [January 14, 2023, 2:37am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233 "2023-01-14T02:37:10Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 15, 2023, 12:34am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/21 "2023-01-15T00:34:18Z")

</div>

What I do with unknown displays, is write a sketch that cycles through the segments/digits in linear order, and very slow. In one difficult case, I made it scan one segment at a time, controlled by a pushbutton to give me time to think/write what I see.

At the end of that, I have a verified "assignment table" to use when writing the code.

I wouldn't worry too much about the LED voltage if they do illuminate satisfactorily.

You **do** have current limiting resistors on the segments, right?

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [January 15, 2023, 12:45am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/22 "2023-01-15T00:45:46Z")

</div>

> [@RElectronics](#):
>
> PD5 is pin 22 which connects to TXLED and PD4 is pin 25 which connects to digital pin 4, looking at my schematic and the atmega32u4 pinout that part seems fine. I

I was referring to the pin numbers in the code you posted not matching the schematic.

---

<div class="post-metadata">

**Author:** ![RElectronics](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@RElectronics](https://forum.arduino.cc/u/RElectronics)\
**Post date:** [January 15, 2023, 1:11am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/23 "2023-01-15T01:11:20Z")

</div>

I have 2.2k resistors on the base of the pnp transistors and a 0 ohm resistor on the collector as per the recommendation of the review request I posted on the EEVblog, worse case scenario I can easily flip the two if need be.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 15, 2023, 1:12am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/24 "2023-01-15T01:12:52Z")

</div>

Neither of those are segment current limiting resistors. Flipping them won't help.

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [January 15, 2023, 1:16am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/25 "2023-01-15T01:16:13Z")

</div>

Your segment resistors are here…

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/9/c/3/9c327911335960b58ccbeeb01fb818ca51d8856a.jpeg)

Not sure why the DP needs 6K8

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 15, 2023, 1:26am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/26 "2023-01-15T01:26:56Z")

</div>

> [@lastchancename](#):
>
> Not sure why the DP needs 6K8

Oh, boy, I didn't see that. I think the modules must be shown incorrectly. Strangely the datasheet agrees. Wired that way, only one of the 3 LEDs for each segment would conduct.

But in many larger modules, LEDs are series wired because they have to be multiple to cover the entire segment area. However the DP is small so only needs one LED. The voltage drop is lower, so a higher value resistor is needed for the same current.

Anybody else think the module LED wiring is strange? 😕  
[https://datasheet.lcsc.com/lcsc/1811131731\_Shenzhen-Zhihao-Elec-FJ11001BH\_C12189.pdf](https://datasheet.lcsc.com/lcsc/1811131731_Shenzhen-Zhihao-Elec-FJ11001BH_C12189.pdf)

If they really are double in series, the LED voltage drop combined could exceed Vcc which is a problem.

For that reason, a voltage measurement might actually be wise.

---

<div class="post-metadata">

**Author:** ![madmark2150](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@madmark2150](https://forum.arduino.cc/u/madmark2150)\
**Post date:** [January 15, 2023, 1:39am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/27 "2023-01-15T01:39:16Z")

</div>

There are TWO 2V4 green LEDS in series for a total of 4V8 needed. Easy on a 5V0 system, not so much on a 3V3.  
Red v green are DIFFERENT in more ways than just emission spectra. Engineering is checking ALL params when subbing parts.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [January 15, 2023, 1:57am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/28 "2023-01-15T01:57:15Z")

</div>

Note that the schematic being referenced is the original from the instructables project. The OP posted a different schematic in the PDF file in the original post.

 ![digital_clock-4](https://europe1.discourse-cdn.com/arduino/original/4X/d/7/c/d7ce1388ce07372dace33e655a0c9521f69e368c.jpeg)

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [January 15, 2023, 2:09am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/29 "2023-01-15T02:09:27Z")

</div>

> [@david\_2018](#):
>
> the original from the instructables project. The OP posted a different schematic in the PDF file in the original post.

That’s helpful ! Thanks, so we ignore one of them… but which one ?

---

<div class="post-metadata">

**Author:** ![RElectronics](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@RElectronics](https://forum.arduino.cc/u/RElectronics)\
**Post date:** [January 15, 2023, 2:48am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/30 "2023-01-15T02:48:58Z")

</div>

Alright, so I found some code online that can potentially help solve my issue or at the very least is a step in the right direction. I realize now that even though I am using 4 individual displays, they are acting as combined 4 digit 7 segment display (quite obvious I know, but this is all a learning process for me clearly). I wrote some code here:

```cpp
#include "SevSeg.h"
SevSeg sevseg; 

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

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

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

```

So all 4 segments do turn on, however segment E is the one that isn't turning on, making numbers 2, 6, and 8 missing a segment (Numbers 1,3,5,7, and 9 are all good). Putting in 0000 for the numbers doesn't really work either, segment U30 on my schematic doesn't turn on at all, and U31-U33 turn on but segment E is missing. I checked continuity with a multimeter and it was present, so I'm thinking my pin assignment for pin E is wrong.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [January 15, 2023, 3:08am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/31 "2023-01-15T03:08:42Z")

</div>

There is not a pin number defined for the TX LED Actually the pin number is 30, or LED\_BUILTIN\_TX. TXLED0 is a macro to output a LOW to the pin, TXLED1 is a macro to output a HIGH.

```cpp
#define TXLED0 PORTD |= (1<<5)
#define TXLED1 PORTD &= ~(1<<5)
```

You really should have breadboarded this and done some testing before building the hardware, you may need to do a bit of re-wiring to make it work with a library. See above, use LED\_BUILTIN\_TX instead of TXLED0.

The way you have all the LED segments wired to a single port would make it easy to directly write to the port, but that is a rather advanced bit of coding.

---

<div class="post-metadata">

**Author:** ![RElectronics](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@RElectronics](https://forum.arduino.cc/u/RElectronics)\
**Post date:** [January 15, 2023, 9:31pm UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/32 "2023-01-15T21:31:20Z")

</div>

> [@david\_2018](#):
>
> LED\_BUILTIN\_TX

Changing pin E to LED\_BUILTIN\_TX does work, however as soon as I change one of the four numbers the display doesn't show those numbers properly

```arduino
#include "SevSeg.h"
SevSeg sevseg; 

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

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

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

The display shows 3 on digit 1, and the remaining 3 digits show 3 with segment e on. I think my pin assignment for pin C is wrong, I made it digital pin 0 which is RX on the arduino leonardo. 

```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [January 15, 2023, 9:55pm UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/33 "2023-01-15T21:55:12Z")

</div>

Try hardwareConfig = P\_TRANSISTORS instead of COMMON\_ANODE.

---

<div class="post-metadata">

**Author:** ![RElectronics](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@RElectronics](https://forum.arduino.cc/u/RElectronics)\
**Post date:** [January 15, 2023, 10:13pm UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/34 "2023-01-15T22:13:25Z")

</div>

Tried that seemed to somewhat work but now if I put 2223, the displays show 3222

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [January 15, 2023, 10:27pm UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/35 "2023-01-15T22:27:20Z")

</div>

Try changing the number to 1234 and see which order they are display in, the library may be going the opposite direction than you are.

---

<div class="post-metadata">

**Author:** ![RElectronics](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@RElectronics](https://forum.arduino.cc/u/RElectronics)\
**Post date:** [January 15, 2023, 10:33pm UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/37 "2023-01-15T22:33:17Z")

</div>

Okay, got it to work had to change the digit pins from 8,10,9,11 to 11,10,8,9

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [January 15, 2023, 10:49pm UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/38 "2023-01-15T22:49:32Z")

</div>

hehe, now read back over the thread, and see what you learned.

Cheers

---

<div class="post-metadata">

**Author:** ![RElectronics](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@RElectronics](https://forum.arduino.cc/u/RElectronics)\
**Post date:** [January 15, 2023, 11:30pm UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/39 "2023-01-15T23:30:31Z")

</div>

I learned that I really need a better understanding of my pin assignments... still confused as to how I'm going to display the time from what I can find online a lot of the example code has numerous errors, I tried creating an array with my pin definitions of the original code I found on instructables but that didn't seem to work either.

---

<div class="post-metadata">

**Author:** ![RElectronics](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@RElectronics](https://forum.arduino.cc/u/RElectronics)\
**Post date:** [January 16, 2023, 12:14am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/40 "2023-01-16T00:14:35Z")

</div>

This is a follow up with the RTC custom PCB thread I made here: [Issue coding RTC 7 segment clock](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233)

For my RTC I'm using the DS1307, and when I upload the code I get this as my output in the serial monitor.

The schematic for my PCB can be found here: [Digital Clock.pdf | DocDroid](https://www.docdroid.net/F9Y5UgL/digital-clock-pdf)

Here's the front copper of the PCB  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/2/b/b/2bbac1319ff7763c6540cf9851a78cd3e0ae4d7a.png)

Originally I had capacitors connected to the crystal as I was using a different RTC IC, then changed to the DS1307. I updated the schematic, but then forgot to update the PCB (bad mistake I know), so I'm wondering if the unpopulated capacitors C28 and C29 for crystal Y21 is causing issues. This is the serial monitor output:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/9/4/6/94683f080e9d87b0402775861d003a55bfde3b01.png)

Here is the code btw:

```cpp
//Arduino 1.0+ Only
//Arduino 1.0+ Only
// pre-set the time in the void then use reset button to set it!

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

void setup(){
  Wire.begin();
  Serial.begin(9600);
      
  setDateTime(); //MUST CONFIGURE IN FUNCTION
printDate();
Serial.println("loopstart");
}

void loop(){
  printDate();
  delay(1000);
}

void setDateTime(){

  byte second = 00; //0-59
  byte minute = 35; //0-59
  byte hour = 15; //0-23
  byte weekDay = 7; //1-7
  byte monthDay = 15; //1-31
  byte month = 1; //1-12
  byte year = 23; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();
}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG 3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 16, 2023, 12:20am UTC](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233/41 "2023-01-16T00:20:55Z")

</div>

flagged as duplicate thread

[Previous page](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233.md?page=1)

[Next page](https://forum.arduino.cc/t/issue-coding-rtc-7-segment-clock/1077233.md?page=3)
