Project updated to beta 6.2 (released on March 5th, 2022)
This project consists on cycling through all the output ports on a 74HC595 to power on LEDs connected to each pin using the Meadow.Foundation library. The intention of this project is to familiarize yourself with using shift registers connected to a Meadow to expand its number of ports.Everything you need to build this project is included in the Wilderness Labs Meadow F7 w/Hack Kit Pro. We'll see how easy is to program these peripherals using Meadow.Foundation.
Shift registers are used to increase the number of outputs on a micro controller by using I2C or SPI interfaces. In the case of the 74xx595 series of shift registers, the SPI interface is used to output a series of bits that are then latched to the output pins of the chip.
Meadow.Foundation: A platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.
If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LEDproject to properly set up your development environment.
Step 1 - Assemble the circuitTo connect the 74HC595 chip, consider the Pin Datasheet below. The necessary pins for the shift register to work is wire up the GND and VCC for power (it works for both 3.3V and 5V). SER is the serial input pin, where set the bits of data along with the shift register clock (SRCLK). When setting all 8 bits, you enable the Latch pin (SRCLK) to copy all the 8 bit values to the latch register.
For this project, you will need to wire up the Output Enable (OE) pin, to see the output changes for when the bit values are latched to the shift register. This pin is an active low, so it needs to be connected to ground. For more information, you can find the data sheet here.
Now that you've gone through the Pin-out on the 74HC595, build the circuit on a breadboard along with your Meadow. Follow the diagram below, and make sure you're connecting everything exactly as illustrated.
Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it ShiftRegisterLeds.
Step 3 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
Step 4 - Write the code for ShiftRegisterLedsCopy the following code below:
// public class MeadowApp : App<F7Micro, MeadowApp> <- If you have a Meadow F7v1.*
public class MeadowApp : App<F7MicroV2, MeadowApp>
{
x74595 shiftRegister;
public MeadowApp()
{
Initialize();
TestX74595();
}
void Initialize()
{
var onboardLed = new RgbPwmLed(
device: Device,
redPwmPin: Device.Pins.OnboardLedRed,
greenPwmPin: Device.Pins.OnboardLedGreen,
bluePwmPin: Device.Pins.OnboardLedBlue);
onboardLed.SetColor(Color.Red);
shiftRegister = new x74595(
device: Device,
spiBus: Device.CreateSpiBus(),
pinChipSelect: Device.Pins.D03,
pins: 8);
onboardLed.SetColor(Color.Green);
}
void TestX74595()
{
shiftRegister.Clear(true);
while (true)
{
shiftRegister.Clear();
for(int i = 0; i < shiftRegister.Pins.AllPins.Count; i++)
{
shiftRegister.WriteToPin(shiftRegister.Pins.AllPins[i], true);
Thread.Sleep(500);
shiftRegister.WriteToPin(shiftRegister.Pins.AllPins[i], false);
}
}
}
}
In the MeadowApp's constructor the first thing the app does is initialize a x74595 object, passing the IODevice which in this case is the Meadow F7 board, an SPI bus, which we create by doing Device.CreateSpiBus()
, the pin D03 for the Chip Select pin, and finally number of pins which in this case is 8.
In the TestX74595
method, we first clear the x74595 from any values by doing shiftRegister.Clear();
and in the infinite while loop, we change the value of one pin of the shift register to true for 500ms ( or half a second), and then we set it to false before going to the next iteration to update the next pin, lighting up the LEDs connected to it one by one.
Click the Run button in Visual Studio. It should look like to the following GIF:
This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.
- It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
- The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.
Comments