This project consists on cycling through all the output ports on a 74HC595 to power on LEDs connected to each pin using the Netduino.Foundation library. The intention of this project is to familiarize yourself with using shift registers connected to a Netduino to expand its number of ports.
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.
Netduino.Foundation is a platform for quickly and easily build connected things using the .NET MicroFramework on Netduino. Created by Wilderness Labs, it's completely open source and maintained by the Netduino community.
If you're new in Netduino development, I suggest you go to the Getting started with Netduino project 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 Netduino. Follow the diagram below, and make sure you're connecting everything exactly as illustrated.
Note: The Netduino is powering the breadboard with 3.3V, but the shift register is able to work with 5V as well.
Step 2 - Create a Netduino ProjectCreate a Netduino project in Visual Studio 2015 for Windows or the latest Visual Studio for Mac; name the project x74595_Sample.
Step 3 - Add the Netduino.Foundation NuGet PackageWindows
Right-click on your x74595_Sample project and click Manage Nuget Packages. In the Browse tab, search for Netduino.Foundation; it should be the first search result. Click the Install button.
macOS
Alt-click on your x74595_Sample project in the Solution Explorer, and click Add => Add Nuget Package to open the NuGet Package window. Search for the Netduino.Foundation package and click Add Package to add it to your project.
Add App Class
For this project, we implement a common App software pattern that manages all the peripherals and main logic.
Add a new App class to your project, and paste the following code:
using System.Threading;
using Netduino.Foundation.ICs.IOExpanders.x74595;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
namespace Blinky_x74595
{
public class App
{
x74595 shiftRegister;
public App()
{
InitializePeripherals();
}
protected void InitializePeripherals()
{
var config = new SPI.Configuration(SPI_mod: SPI_Devices.SPI1,
ChipSelect_Port: Pins.GPIO_PIN_D8,
ChipSelect_ActiveState: false,
ChipSelect_SetupTime: 0,
ChipSelect_HoldTime: 0,
Clock_IdleState: true,
Clock_Edge: true,
Clock_RateKHz: 10);
shiftRegister = new x74595(8, config);
}
public void Run()
{
shiftRegister.Clear(true);
int index = 0;
while (true)
{
shiftRegister[index] = true;
Thread.Sleep(500);
shiftRegister[index] = false;
index = index == 8 ? 0 : index + 1;
}
}
}
}
In the InitializePeripherals method, we first create a SPI Configuration object (var config), specifying the pins used from the Netduino, Clock parameters like rate in Khz, idle state and edge.
Once you have the shift register configuration object, instanciate a x74595 object (var shiftRegister) passing the number of bits available along with config.
In the App's Run method, we first clear the x74595 from any values by doing shiftRegister.Clear(true);
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.
Program Class
Finally, create a new App class object and invoke the Run method. Your code should look like this:
using System.Threading;
namespace Blinky_x74595
{
public class Program
{
public static void Main()
{
App app = new App();
app.Run();
Thread.Sleep(Timeout.Infinite);
}
}
}
Step 5 - Run the projectClick the run button in Visual Studio to see your LEDs light up as the program cycles through all the Output ports on your shift register:
This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Netduino.Foundation.
- It comes with a Huge Peripheral Driver Library with drivers for the most common sensors and peripherals available in the market.
- All the peripheral drivers are simplified with built-in functionality, exposed by a clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and always excited to help new-comers and discuss new projects.
Comments