This example shows how to use the Grove LED Bar on the Pynq-Z1 board. The LED bar has 10 LEDs: 8 green LEDs, 1 orange LED, and 1 red LED. The brightness for each LED can be set independantly.
For this notebook, a PYNQ Arduino shield is also required. The LED bar is attached to the G4 connection on the shield. The grove LED bar also works with PMODA and PMODB on the Pynq-Z1 board.
In :# Make sure the base overlay is loaded from pynq import Overlay Overlay("base.bit").download()
In :from pynq.iop import Grove_LEDbar from pynq.iop import ARDUINO from pynq.iop import ARDUINO_GROVE_G4 # Instantiate Grove LED Bar on Arduino shield G4 ledbar = Grove_LEDbar(ARDUINO,ARDUINO_GROVE_G4) ledbar.reset()
In :from time import sleep # Light up different bars in a loop for i in range(2): ledbar.write_binary(0b1010100000) sleep(0.5) ledbar.write_binary(0b0000100100) sleep(0.5) ledbar.write_binary(0b1010101110) sleep(0.5) ledbar.write_binary(0b1111111110) sleep(0.5)
In :# Brightness 0-255 HIGH = 0xFF MED = 0xAA LOW = 0x01 OFF = 0X00 brightness = [OFF, OFF, OFF, LOW, LOW, MED, MED, HIGH, HIGH, HIGH] ledbar.write_brightness(0b1111111111,brightness)
A number or level of LEDs can be turned on, started from either end of the LED bar. For example, this feature could be used to indicate the level of something being measured.
write_level(level, bright_level, green_to_red)
For example, ledbar.write_level(5,4,1) will light 5 LEDs, to brightness 4 (out of 10) and will start from the Green LED (the LED furthest away from Grove connector on the LED bar module.)
In :for i in range (1,11): ledbar.write_level(i,3,0) sleep(0.3) for i in range (1,10): ledbar.write_level(i,3,1) sleep(0.3)
In :from pynq.board import Button btns = [Button(index) for index in range(4)] i = 1 ledbar.reset() done = False while not done: if (btns.read()==1): sleep(0.2) ledbar.write_level(i,2,1) i = min(i+1,9) elif (btns.read()==1): sleep(0.2) i = max(i-1,0) ledbar.write_level(i,2,1) elif (btns.read()==1): ledbar.reset() done = True