Skip to content

Commit 7a166be

Browse files
authored
Merge pull request #22 from Chao8219/main
add daisy chain example
2 parents 2296dce + cd3633b commit 7a166be

7 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# About
2+
3+
The example here shows the wiring and codes for daisy-chained shift registers.
4+
5+
The following items are used:
6+
- Two Shift Registers: [74HC595](https://www.adafruit.com/product/450)
7+
- One Level Shifter: [74AHCT125](https://www.adafruit.com/product/1787)
8+
- One LED Display: [10 Segment Light Bar LED](https://www.adafruit.com/product/1815)
9+
- One microcontroller board: [ItsyBitsy M0 Express](https://www.adafruit.com/product/3727)
10+
11+
In addition, some other items are needed as well:
12+
- Through-hole 2K Resistor: [CFR-25JR-52-2K](https://www.digikey.com/en/products/detail/yageo/CFR-25JR-52-2K/11981)
13+
- Jumper wires
14+
15+
The idea is to let the microcontroller to communicate with the shift registers through SPI, and then control 10 LEDs.
16+
The level shifter **74AHCT125** helps to translate 3.3V signal to 5.0V, since this example is trying to use 5.0V to drive LEDs and chips.
17+
If one uses 3.3V for all LEDs and chips, the level shifter is not going to be needed.
18+
19+
For the daisy chain connection, this guide, [SPI interface Tutorial](https://www.best-microcontroller-projects.com/spi-interface.html), can provide some basic ideas.
20+
The guide, [Serial to Parallel Shifting-Out with a 74HC595](https://www.arduino.cc/en/Tutorial/Foundations/ShiftOut), can also provide some connection details for the 74HC595 chip.
21+
22+
# Wiring
23+
24+
With the board `Itsy Bitsy M0 Express`, pins can be connected as follows:
25+
26+
![Itsy Bitsy M0 Express with Two Shift Registers, LEDs, and Level Shifter Fritzing](figs/ItsyBitsyM0_w_LEDs_and_two_shift_registers_bb.png)
27+
28+
In the wiring, the ItsyBitsy M0 is not powered, given that one can power it up through USB cable when programming it.
29+
30+
Also, one may notice that, for the first 8 LEDs (from left to right), the pins from the 74HC595 at the *end* of the daisy chain are fully utilized, which means that the end of the daisy chain is actually the first device, the index 0 device.
31+
32+
Therefore,
33+
- for pin index 0~7, the value will be sent to the device at the end of the daisy chain
34+
- for pin index 8~15, the value will be sent to the device the second end of the daisy chain (which is the head of the daisy chain in this example)
35+
36+
# Coding
37+
38+
The example code can be found in `74hc595_daisy_chain.py`.
39+
Here are some notes about the codes:
40+
41+
## Import Modules
42+
43+
```python
44+
import time
45+
import board
46+
import digitalio
47+
import adafruit_74hc595
48+
```
49+
50+
## Instantiate Class
51+
52+
One need to provide the number of shift registers when instantiating the class.
53+
54+
```python
55+
# note: D2 port is close to SCK and MOSI pins for Itsy Bitsy M0
56+
latch_pin = digitalio.DigitalInOut(board.D2)
57+
sr = adafruit_74hc595.ShiftRegister74HC595(board.SPI(), latch_pin, number_of_shift_registers=2)
58+
59+
shift_register_pin_num = 10
60+
shift_register_pins = [sr.get_pin(n) for n in range(shift_register_pin_num)]
61+
```
62+
63+
## Do Something with the Pins
64+
65+
One simple example is to blink all LEDs together:
66+
67+
```python
68+
while True:
69+
# turn all LEDs on
70+
for i in range(shift_register_pin_num):
71+
shift_register_pins[i].value = True
72+
time.sleep(0.5)
73+
74+
# turn all LEDs off
75+
for i in range(shift_register_pin_num):
76+
shift_register_pins[i].value = False
77+
time.sleep(0.5)
78+
```
79+
80+
A more complicated example is to turn on/off each LEDs one-by-one:
81+
```python
82+
bar_ind_current = 0 # current index
83+
shift_register_pins[bar_ind_current].value = True
84+
85+
while True:
86+
# iterate through every LED
87+
bar_ind_last = bar_ind_current
88+
bar_ind_current = bar_ind_last + 1
89+
if bar_ind_current <= (shift_register_pin_num-1):
90+
shift_register_pins[bar_ind_last].value = False
91+
shift_register_pins[bar_ind_current].value = True
92+
else:
93+
bar_ind_current = 0
94+
shift_register_pins[bar_ind_last].value = False
95+
shift_register_pins[bar_ind_current].value = True
96+
time.sleep(0.1)
97+
```
98+
99+
Here is the result one can get:
100+
101+
![Itsy Bitsy M0 Express with Two Shift Registers, LEDs, and Level Shifter Demo](figs/ItsyBitsyM0_w_LEDs_and_two_shift_registers.gif)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: 2021 Zichao Hou
2+
3+
SPDX-License-Identifier: MIT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: 2021 Zichao Hou
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import digitalio
7+
import adafruit_74hc595
8+
9+
# note: D2 port is close to SCK and MOSI pins for Itsy Bitsy M0
10+
latch_pin = digitalio.DigitalInOut(board.D2)
11+
sr = adafruit_74hc595.ShiftRegister74HC595(
12+
board.SPI(), latch_pin, number_of_shift_registers=2
13+
)
14+
15+
shift_register_pin_num = 10
16+
shift_register_pins = [sr.get_pin(n) for n in range(shift_register_pin_num)]
17+
18+
# turn all pins off
19+
for pin in shift_register_pins:
20+
pin.value = False
21+
22+
bar_ind_current = 0 # current index
23+
shift_register_pins[bar_ind_current].value = True
24+
25+
while True:
26+
# iterate through every LED
27+
bar_ind_last = bar_ind_current
28+
bar_ind_current = bar_ind_last + 1
29+
if bar_ind_current <= (shift_register_pin_num - 1):
30+
shift_register_pins[bar_ind_last].value = False
31+
shift_register_pins[bar_ind_current].value = True
32+
else:
33+
bar_ind_current = 0
34+
shift_register_pins[bar_ind_last].value = False
35+
shift_register_pins[bar_ind_current].value = True
36+
time.sleep(0.1)
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: 2021 Zichao Hou
2+
3+
SPDX-License-Identifier: MIT
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: 2021 Zichao Hou
2+
3+
SPDX-License-Identifier: MIT

0 commit comments

Comments
 (0)