Try to convert my code into Micropython

Hello everyone,

I have a code running correctly on my Arduino that count over 5 seven segment displays using a library. Can someone help me to format this code so it can run micropython?
I can understand that the library that i have include will oppose a problem but is there an equivalent for micropython or is it possible rebuild it without a library?

I've posted here after searching on websites to find information also looked for tutorials for shift registers without success.

I hope to hear from you soon.

#include <ShiftRegister74HC595.h>
// create shift register object (number of shift registers, data pin, clock pin, latch pin)

ShiftRegister74HC595<2> sr (6, 4, 5);

unsigned long countnumber;
int  firstnum=0;
int  secondnum=0;
int  thirdnum=0;
int  fournum=0;
int  fivenum=0;
int  sixnum=0;
int  sevennum=0;
int  eightnum=0;

int value,digit1,digit2,digit3,digit4; 
uint8_t  digits[] = { B10111111, //0 Order is dp g f e d c b a
                      B10000110, //1 
                      B01011011, //2
                      B11001111, //3 
                      B11100110, //4
                      B11101101, //5
                      B11111101, //6 
                      B10000111, //7
                      B11111111, //8
                      B11101111  //9
                     };
                        
void setup() {
  Serial.begin(115200);
  
  
}



void incrementer () {
 
  countnumber = millis();

  firstnum = countnumber / 10000000 % 10;
  secondnum = countnumber / 1000000 % 10;
  thirdnum = countnumber / 100000 % 10;
  fournum = countnumber / 10000 % 10;
  fivenum = countnumber / 1000 % 10;
  sixnum = countnumber / 100 % 10;
  sevennum = countnumber / 10 % 10;
  eightnum = countnumber % 10;  
    
  uint8_t numberToPrint[]= {digits[sixnum],digits[fivenum]};
  sr.setAll(numberToPrint);  
}



void showNumber(int num)
{
    digit4 = num % 10 ;
    digit3 = (num / 10) % 10 ;
    digit2 = (num / 100) % 10 ;
    digit1 = (num / 1000) % 10 ;
    fournum = (num / 10000) % 10;

    
     
    //Send them to 7 segment displays
    uint8_t numberToPrint[]= {digits[digit1],digits[fournum]};
    sr.setAll(numberToPrint);  
}


void loop() {

  incrementer();
 
  //showNumber(1316);   
}

Google "74HC595 micropython library"
The first few hits returned Micropython libraries to drive 74HC595s.

I don't know if someone already used this library micropython-74hc595, i have a hard time understanding this lib. i've set my Three pins (data , clock , latch).

i'm using a tpic6c596d like on my previous code

Any help would be much appreciated

error:

Traceback (most recent call last):
File "", line 11, in
File "/lib/sr_74hc595_bitbang.py", line 68, in clear
RuntimeError: srclr pin is required

you can find my code + wiring diagram

from machine import Pin
from sr_74hc595_bitbang import SR

ser = Pin(13, Pin.OUT)
srclk = Pin(14, Pin.OUT)
rclk = Pin(15, Pin.OUT)

# construct without optional pins
sr = SR(ser, srclk, rclk)

sr.clear()  # raises RuntimeError because you haven't provide srclr pin
sr.enable() # raises RuntimeError because you haven't provide oe pin

# reconstruct with all pins
#oe = Pin(33, Pin.OUT, value=0)    # low enables output
#srclr = Pin(32, Pin.OUT, value=1) # pulsing low clears data

#sr = SR(ser, srclk, rclk, srclr, oe)

sr.bit(1)  # send high bit, do not latch yet
sr.bit(0)  # send low bit, do not latch yet
sr.latch() # latch outputs, outputs=0000_0010

sr.bit(1, 1) # send high bit and latch, outputs=0000_0101
sr.bit(0, 1) # send low bit and latch, outputs=0000_1010

sr.bits(0xff, 4) # send 4 lowest bits of 0xff (sends 0x0f), outputs=1010_1111

sr.clear(0) # clear the memory but don't latch yet
sr.latch()  # next latch shows the outputs have been reset

sr.bits(0b1010_1010, 8) # write some bits
sr.clear()  # clear the memory and latch, outputs have been reset

sr.enable()  # outputs enabled
sr.enable(0) # outputs disabled

Try changing this:

# construct without optional pins
sr = SR(ser, srclk, rclk)

to:

# construct without optional pins
sr = SR(ser, srclk, rclk, srclr=None, oe=None))

and comment out:

sr.clear()  # raises RuntimeError because you haven't provide srclr pin
sr.enable() # raises RuntimeError because you haven't provide oe pin

Hi Emilyjane,

Thank you for your assistance but is it give me the same error,
I would like to test to display a number

from machine import Pin
from sr_74hc595_bitbang import SR

ser = Pin(13, Pin.OUT)
srclk = Pin(14, Pin.OUT)
rclk = Pin(15, Pin.OUT)


# construct without optional pins
sr = SR(ser, srclk, rclk, srclr=None, oe=None)

#sr.clear()  # raises RuntimeError because you haven't provide srclr pin
#sr.enable() # raises RuntimeError because you haven't provide oe pin

# reconstruct with all pins
#oe = Pin(33, Pin.OUT, value=0)    # low enables output
#srclr = Pin(32, Pin.OUT, value=1) # pulsing low clears data

#sr = SR(ser, srclk, rclk, srclr, oe)

sr.bit(1)  # send high bit, do not latch yet
sr.bit(0)  # send low bit, do not latch yet
sr.latch() # latch outputs, outputs=0000_0010

sr.bit(1, 1) # send high bit and latch, outputs=0000_0101
sr.bit(0, 1) # send low bit and latch, outputs=0000_1010

sr.bits(0xff, 4) # send 4 lowest bits of 0xff (sends 0x0f), outputs=1010_1111

sr.clear(0) # clear the memory but don't latch yet
sr.latch()  # next latch shows the outputs have been reset

sr.bits(0b1010_1010, 8) # write some bits
sr.clear()  # clear the memory and latch, outputs have been reset

sr.enable()  # outputs enabled
sr.enable(0) # outputs disabled

I'll look closer at the library. Give me a while. I'm busy with something else right now.

I just noticed you are using a TPIC6B595 not a 74HC595. You need to post a complete schematic of the way your project is actually wired.
You need some level shifters to interface with the 3v3 logic on the Pico.

I'm providing an external 5 volt for the shift registers, the Raspberry pi Pico only send the data, clock and latch signal to the IC.

pin 1 -> not connected
pin 2 vcc -> external 5 volt
pin 3 SER -> for the data pin 13 on the raspberry pi
pin 8 -> external 5 volt
pin 9 -> ground
pin 10 -> ground
pin 11 -> ground
pin 12 rclk -> for the latch pin 15 on the raspberry pi
pin 13 srclk -> for the clock pin 14 on the raspberry pi
pin 18 -> for daisy chain multiple register
pin 19 -> ground
pin 20 -> not connected

TPIC6B595 pinout

TPIC6B595_PinOut-1

and the 74HC595 pinout

I've convert those pins based on this thread 74HC595 and TPIC6B595

Thank you for your time

The logic outputs of the Pico are not the right levels to drive the shift register. The shift registers require a logic 1 of at least 4.25V if powered with 5V.

You did not heed this request. Instead, you posted a kind of "Mensa puzzle" of words and data sheet cut and pastes. That is not useful to most of the people here, schematics are their language and they won't bother to take the 30 minutes it takes to visualize a real schematic from what you posted.

Additionally, schematics often show details that the author is not even aware of, due to their literal nature. And, verbal descriptions and/or pin lists often omit details that conceal the actual problem.

1 Like

@Hafid_EL Finish this puzzle and maybe we can help you.

I feel obligated to mention that you need some current limit resistors for those LEDs should you ever get to the point where you can light them up.

Sorry for the misunderstanding and not providing the correct assets,

I've mange to make it working by changing the wiring and code for one digit without a library,
but can't figure out how to daisy chain 5 ic's in the code to create a counter in milliseconds (10.000 sec.).

i have include the schematic and the code

from machine import Pin
from time import sleep


dataPIN = 13
clockPIN = 14
latchPIN = 15


dataPIN=Pin(dataPIN, Pin.OUT)
latchPIN=Pin(latchPIN, Pin.OUT)
clockPIN=Pin(clockPIN, Pin.OUT)

digits = [
            [0,0,1,1,1,1,1,1], #0 Order is dp g f e d c b a
            [0,0,0,0,0,1,1,0], #1 
            [0,1,0,1,1,0,1,1], #2
            [0,1,0,0,1,1,1,1], #3
            [0,1,1,0,0,1,1,0], #4
            [0,1,1,0,1,1,0,1], #5
            [0,1,1,1,1,1,0,1], #6 
            [0,0,0,0,0,1,1,1], #7
            [0,1,1,1,1,1,1,1], #8
            [0,1,1,0,1,1,1,1]  #9
         ];



def shift_update(input,data,clock,latch):
  #put latch down to start data sending
  clock.value(0)
  latch.value(0)
  clock.value(1)
  
  #load data in
  for i in range(8):
    clock.value(0)
    data.value(int(input[i]))
    clock.value(1)

  #put latch up to store data on register
  clock.value(0)
  latch.value(1)
  clock.value(1)
  
#testing one digit   
#shift_update(digits[5],dataPIN,clockPIN,latchPIN)
  
for i in range(10):
    
    shift_update(digits[i],dataPIN,clockPIN,latchPIN)
    sleep(.001)

Thank you for any help you can offer.

I saw a RPi Pico on your diagram.
Just for your information - you can use C++ in Arduino IDE with this board.

Do you

clear the latch
for each of 5 digits
{
  write a byte
}
set the latch

?

Have you looked at the 595 data sheet to see how it works?

The data sheet I have for your device indicates that it needs a Vcc of 4.5V-5.5V. Maybe it will operate off 3v3, maybe not reliably.

Thanks for the schematic but you don't show how you have the cathodes of your LEDs wired to the 6B595's.

Did you try getting a test program like this to work satisfactorily?

from machine import Pin
from sr_74hc595_bitbang import SR

ser = Pin(13, Pin.OUT)
srclk = Pin(14, Pin.OUT)
rclk = Pin(15, Pin.OUT)

# construct without optional pins
sr = SR(ser, srclk, rclk, srclr=None, oe=None)


sr.bit(1)  # send high bit, do not latch yet
sr.bit(0)  # send low bit, do not latch yet
sr.latch() # latch outputs, outputs=0000_0010

sr.bit(1, 1) # send high bit and latch, outputs=0000_0101
sr.bit(0, 1) # send low bit and latch, outputs=0000_1010

sr.bits(0xff, 4) # send 4 lowest bits of 0xff (sends 0x0f), outputs=1010_1111

Edit: I see you have incorporated some of the library in your code. I'll read it and see if I have any comments.

Thank you all for your feedbacks,

Once i get the quad level shifter that you recommended i will hook them up. But for now it seems to tolerate the 3.3v.

For the schematic i didn't wired the cathodes but on my breadboard i wired them respectively starting from A to G + DP

The code that you posted using the sr_74hc595_bitbang seems to work but not all the segment lighted up a-c-dp are turn off, i'm not really have the knowledge using hex bits, i'm more confortable using this approach, but i'm ok to convert into hex bits if necessary :

digits = [
[0,0,1,1,1,1,1,1], #0 Order is dp g f e d c b a
[0,0,0,0,0,1,1,0], #1
[0,1,0,1,1,0,1,1], #2
[0,1,0,0,1,1,1,1], #3
[0,1,1,0,0,1,1,0], #4
[0,1,1,0,1,1,0,1], #5
[0,1,1,1,1,1,0,1], #6
[0,0,0,0,0,1,1,1], #7
[0,1,1,1,1,1,1,1], #8
[0,1,1,0,1,1,1,1] #9
];

This should turn on all 7 segments + DP

sr.bits(0xff, 8,True) # send 8 bits of 0xff (sends 0xff), outputs=1111_1111 & latch

You could think about your list as being integers like this if you wanted to.

digits = [
            0b00111111, #0
            0b00000110, #1
            0b01011011, #2
            0b01001111, #3
            0b01100110, #4
            0b01101101, #5
            0b01111101, #6
            0b00000111, #7
            0b01111111, #8
            0b01101111, #9
            0b10000000  #DP
        
        ]

Thank you for the example, maybe i'm wrong but i don't think this library allows me to chain multiple ic's.