Arduino Lesson 4. Eight LEDs and a Shift Register



Similar documents
Arduino Lesson 9. Sensing Light

Arduino Lesson 5. The Serial Monitor

Arduino Lesson 13. DC Motors. Created by Simon Monk

Arduino Lesson 14. Servo Motors

Arduino Lesson 1. Blink

Arduino Lesson 0. Getting Started

Arduino Lesson 16. Stepper Motors

Arduino Lesson 17. Sending Movement Detector

Adafruit's Raspberry Pi Lesson 9. Controlling a DC Motor

Eric Mitchell April 2, 2012 Application Note: Control of a 180 Servo Motor with Arduino UNO Development Board

Character LCDs. Created by Ladyada. Last updated on :45:29 PM EDT

Your Multimeter. The Arduino Uno 10/1/2012. Using Your Arduino, Breadboard and Multimeter. EAS 199A Fall Work in teams of two!

DS1307 Real Time Clock Breakout Board Kit

IR Communication a learn.sparkfun.com tutorial

C4DI Arduino tutorial 4 Things beginning with the letter i

2.2" TFT Display. Created by Ladyada. Last updated on :15:09 PM EDT

Playing sounds and using buttons with Raspberry Pi

Sending an SMS with Temboo

Theory and Practice of Tangible User Interfaces. Thursday Week 2: Digital Input and Output. week. Digital Input and Output. RGB LEDs fade with PWM

RGB LED Strips. Created by lady ada. Last updated on :00:18 PM EST

Basic Pulse Width Modulation

cs281: Introduction to Computer Systems Lab08 Interrupt Handling and Stepper Motor Controller

A REST API for Arduino & the CC3000 WiFi Chip

Introduction to Arduino

Adafruit MCP9808 Precision I2C Temperature Sensor Guide

TSL2561 Luminosity Sensor

BUILDING INSTRUCTIONS

Adding a Real Time Clock to Raspberry Pi

Set up and Blink - Simulink with Arduino

Adafruit SHT31-D Temperature & Humidity Sensor Breakout

Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing

Introduction to Arduino

Capacitive Touch Sensor Project:

SSH to BeagleBone Black over USB

Arduino project. Arduino board. Serial transmission

How to Move Canon EF Lenses. Yosuke Bando

Lab 6 Introduction to Serial and Wireless Communication

DEPARTMENT OF ELECTRONICS ENGINEERING

Arduino Motor Shield (L298) Manual

CHAPTER 11: Flip Flops

Tiny Arduino Music Visualizer

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

Arduino DUE + DAC MCP4922 (SPI)

Adafruit Proto Shield for Arduino

The components. E3: Digital electronics. Goals:

An Introduction To Simple Scheduling (Primarily targeted at Arduino Platform)

Thermistor. Created by Ladyada. Last updated on :30:46 PM EDT

MANUAL FOR RX700 LR and NR

Adafruit's Raspberry Pi Lesson 7. Remote Control with VNC

Basic DC Motor Circuits. Living with the Lab Gerald Recktenwald Portland State University

Basic DC Motor Circuits

Animated Lighting Software Overview

1 Coffee cooling : Part B : automated data acquisition

Programming PIC Microcontrollers in PicBasic Pro Lesson 1 Cornerstone Electronics Technology and Robotics II

Using an IR Remote with a Raspberry Pi Media Center

7 OUT1 8 OUT2 9 OUT3 10 OUT4 11 OUT5 12 OUT6 13 OUT7 14 OUT8 15 OUT9 16 OUT10 17 OUT11 18 OUT12 19 OUT13 20 OUT14 21 OUT15 22 OUT16 OUT17 23 OUT18

1.8" TFT Display Breakout and Shield

MODULE BOUSSOLE ÉLECTRONIQUE CMPS03 Référence :

TEECES DOME LIGHTING SYSTEMS

Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout

Crazy Alarm Clock L A K S H M I M E Y Y A P P A N J A M E S K A Y E W I L L I A M D I E H L C O N G C H E N

SYSTEM 4C. C R H Electronics Design

RC Camera Control. User Guide v /20/2012

E-Blocks Easy Internet Bundle

2.3" Monochrome 128x32 OLED Display Module

Lectric Enterprises 5905 Sprucepine Drive Winston Salem, NC Telephone

PICAXE COLOUR SENSOR. revolution. Overview: Contents (AXE045 Colour Sensor): Contents (AXE112S Starter Pack): General Operation:

1. Learn about the 555 timer integrated circuit and applications 2. Apply the 555 timer to build an infrared (IR) transmitter and receiver

UPS PIco. to be used with. Raspberry Pi B+, A+, B, and A. HAT Compliant. Raspberry Pi is a trademark of the Raspberry Pi Foundation

Debouncing Switches. Mechanical switches are one of the most common interfaces to a uc.

TUTORIAL FOR INITIALIZING BLUETOOTH COMMUNICATION BETWEEN ANDROID AND ARDUINO

Android Programming Family Fun Day using AppInventor

Fairchild Solutions for 133MHz Buffered Memory Modules

MCP4725 Digital to Analog Converter Hookup Guide

2013 G Miller. 3 Axis Brushless Gimbal Controller Manual

SYSTEM 45. C R H Electronics Design

4 Character 5x7 LED Matrix Display

E-Blocks Easy RFID Bundle

System theremino MasterDIL-V3

0832 Dot Matrix Green Display Information Board User s Guide

Microcontroller Programming Beginning with Arduino. Charlie Mooney

Surveillance System Using Wireless Sensor Networks

Lecture 7: Programming for the Arduino

1.5" & 2.1" Monochrome 128x64 OLED Display Module

AXE114S BINARY CLOCK. revolution Revolution Education Ltd. Web: Version /09/08 AXE114.PMD.

Cornerstone Electronics Technology and Robotics I Week 15 Voltage Comparators Tutorial

Lesson 8: Simon - Arrays

The Basics of Robot Mazes Teacher Notes

EARTH PEOPLE TECHNOLOGY SERIAL GRAPH TOOL FOR THE ARDUINO UNO USER MANUAL

AR1100 Resistive Touch Screen Controller Guide

Three Arduino Challenges to Connect the Logical World with the Physical One. ISTE Philadelphia

K6002 TEMPERATURE CONTROLLER. Specifications

ECEN 1400, Introduction to Analog and Digital Electronics

Intro to Intel Galileo - IoT Apps GERARDO CARMONA

WHO ANSWERED FIRST? FIND OUT WITH THIS QUIZ BUZZER KIT

How to Make a Pogo Pin Test Jig. Created by Tyler Cooper

Matrix and 7-Segment LED Backpack with the Raspberry Pi

Magic Poker 2 Manual

Introducing AVR Dragon

ARDUINO SEVERINO SERIAL SINGLE SIDED VERSION 3 S3v3 (REVISION 2) USER MANUAL

Transcription:

Arduino Lesson 4. Eight LEDs and a Shift Register Created by Simon Monk Last updated on 2014-09-01 11:30:10 AM EDT

Guide Contents Guide Contents Overview Parts Part Qty Breadboard Layout The 74HC595 Shift Register Arduino Code Brightness Control Other Things to Do 2 3 4 4 4 7 9 12 15 17 Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 2 of 17

Overview In this lesson, you will learn how to use eight large red LEDs with an Arduino without needing to give up 8 output pins! Although you could wire up eight LEDs each with a resistor to an Arduino pin (like we did for an RGB LED in Lesson 2) you would rapidly start to run out of pins on your Arduino. If you don't have a lot of stuff connected to your 'duino it's OK to do so - but often times we want buttons, sensors, servos, etc and before you know it you've got no pins left. So, instead of doing that, you are going to use a chip called the 74HC595 Serial to Parallel Co nverter. This chip has eight outputs (perfect) and three inputs that you use to feed data into it a bit at a time. This chip makes it a little slower to drive the LEDs (you can only change the LEDs about 500,000 times a second instead of 8,000,000 a second) but it's still really really fast, way faster than humans can detect, so it's worth it! Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 3 of 17

Parts To build the project described in this lesson, you will need the following parts. Part Qty 5mm Red LED 8 270 Ω Resistors (red, purple, brown stripes) 8 Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 4 of 17

74HC595 Shift Register 1 Half-size Breadboard 1 Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 5 of 17

Arduino Uno R3 1 Jumper wire pack 1 Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 6 of 17

Breadboard Layout As we have eight LEDs and eight resistors to connect up, there are actually quite a few connections to be made. It is probably easiest to put the 74HC595 chip in first, as pretty much everything else connects to it. Put it so that the little U-shaped notch is towards the top of the breadboard. Pin 1 of the chip is to the left of this notch. Digital 4 from the arduino goes to pin #14 of the shift register Digital 5 from the arduino goes to pin #12 of the shift register Digital 6 from the arduino goes to pin #11 of the shift register All but one of the outputs from the '595 are on the left hand side of the chip, hence, for ease Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 7 of 17

of connection, that is where the LEDs are too. After the chip, put the resistors in place. You need to be careful that none of the leads of the resistors are touching each other. You should check this again, before you connect the power to your Arduino. If you find it difficult to arrange the resistors without their leads touching, then it helps to shorten the leads so that they are lying closer to the surface of the breadboard.next, place the LEDs on the breadboard. The longer positive LED leads must all be towards the chip, whichever side of the breadboard they are on. It now just remains to attach the jumper leads as shown above. Do not forget the one that goes from pin 8 of the IC to the GND column of the breadboard. Load up the sketch listed a bit later and try it out. Each LED should light in turn until all the LEDs are on, and then they all go off and the cycle repeats. Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 8 of 17

The 74HC595 Shift Register Before I go through the code, let's have a quick look at what the chip is doing, so that we can understand what the code has to do. The chip is of a type called a shift register. Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 9 of 17

The shift register holds what can be thought of as eight memory locations, each of which can be a 1 or a 0. Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 10 of 17

To set each of these values on or off, we feed in the data using the 'Data' and 'Clock' pins of the chip. The clock pin needs to receive eight pulses, at the time of each pulse, if the data pin is high, then a 1 gets pushed into the shift register, otherwise a 0. When all eight pulses have been received, then enabling the 'Latch' pin copies those eight values to the latch register. This is necessary, otherwise the wrong LEDs would flicker as the data was being loaded into the shift register. The chip also has an OE (output enable) pin, this is used to enable or disable the outputs all at once. You could attach this to a PWM capable Arduino pin and use 'analogwrite' to control the brightness of the LEDs. This pin is active low, so we tie it to GND. Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 11 of 17

Arduino Code Arduino includes a special function called 'shiftout' that is designed specifically for sending data to shift registers. Here is the full sketch, the discussion of how it works follows on from it. /* Adafruit Arduino - Lesson 4. 8 LEDs and a Shift Register */ int latchpin = 5; int clockpin = 6; int datapin = 4; byte leds = 0; void setup() pinmode(latchpin, OUTPUT); pinmode(datapin, OUTPUT); pinmode(clockpin, OUTPUT); void loop() leds = 0; updateshiftregister(); delay(500); for (int i = 0; i < 8; i++) bitset(leds, i); updateshiftregister(); delay(500); void updateshiftregister() digitalwrite(latchpin, LOW); shiftout(datapin, clockpin, LSBFIRST, leds); digitalwrite(latchpin, HIGH); The first thing we do is define the three pins we are going to use. These are the Arduino Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 12 of 17

digital outputs that will be connected to the latch, clock and data pins of the 74HC595. int latchpin = 5; int clockpin = 6; int datapin = 4; Next, a variable called 'leds' is defined. This will be used to hold the pattern of which LEDs are currently turned on or off. Data of type 'byte' represents numbers using eight bits. Each bit can be either on or off, so this is perfect for keeping track of which of our eight LEDs are on or off. byte leds = 0; The 'setup' function just sets the three pins we are using to be digital outputs. void setup() pinmode(latchpin, OUTPUT); pinmode(datapin, OUTPUT); pinmode(clockpin, OUTPUT); The 'loop' function initially turns all the LEDs off, by giving the variable 'leds' the value 0. It then calls 'updateshiftregister' that will send the 'leds' pattern to the shift register so that all the LEDs turn off. We will deal with how 'updateshiftregister' works later. The loop function pauses for half a second and then begins to count from 0 to 7 using the 'for' loop and the variable 'i'. Each time, it uses the Arduino function 'bitset' to set the bit that controls that LED in the variable 'leds'. It then also calls 'updateshiftregister' so that the leds update to reflect what is in the variable 'leds'. There is then a half second delay before 'i' is incremented and the next LED is lit. void loop() leds = 0; updateshiftregister(); delay(500); for (int i = 0; i < 8; i++) bitset(leds, i); updateshiftregister(); Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 13 of 17

delay(500); The function 'updateshiftregister', first of all sets the latchpin to low, then calls the Arduino function 'shiftout' before putting the 'latchpin' high again. This takes four parameters, the first two are the pins to use for Data and Clock respectively. The third parameter specifies which end of the data you want to start at. We are going to start with the right most bit, which is referred to as the 'Least Significant Bit' (LSB). The last parameter is the actual data to be shifted into the shift register, which in this case is 'leds'. void updateshiftregister() digitalwrite(latchpin, LOW); shiftout(datapin, clockpin, LSBFIRST, leds); digitalwrite(latchpin, HIGH); If you wanted to turn one of the LEDs off rather than on, you would call a similar Arduino function (bitclear) on the 'leds' variable. This will set that bit of 'leds' to be 0 and you would then just need to follow it with a call to 'updateshiftregister' to update the actual LEDs. Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 14 of 17

Brightness Control One pin of the 74HC595 that I have not mentioned is a pin called 'Output Enable'. This is pin 13 and on the breadboard, it is permanently connected to Ground. This pin acts as a switch, that can enable or disable the outputs - the only thing to watch for is it is 'active low' (connect to ground to enable). So, if it is connected to 5V, all the outputs go off. Whereas if it is connected to Ground, those outputs that are supposed to be on are on and those that should be off are off. We can use this pin along with the 'analogwrite' function, that we used back in Lesson 3, to control the brightness of the LEDs using PWM (also see Lesson 3). To do this, all you need to do, is to change the connection to pin 13 of the 74HC595 so that instead of connecting it to Ground, you connect it to pin 3 of the Arduino. The sketch below, will once all the LEDs have been lit gradually fade them back to off. /* Adafruit Arduino - Lesson 4. 8 LEDs and a Shift Register - Brightness */ int latchpin = 5; int clockpin = 6; int datapin = 4; int outputenablepin = 3; byte leds = 0; void setup() pinmode(latchpin, OUTPUT); pinmode(datapin, OUTPUT); pinmode(clockpin, OUTPUT); pinmode(outputenablepin, OUTPUT); void loop() setbrightness(255); leds = 0; updateshiftregister(); delay(500); for (int i = 0; i < 8; i++) bitset(leds, i); Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 15 of 17

updateshiftregister(); delay(500); for (byte b = 255; b > 0; b--) setbrightness(b); delay(50); void updateshiftregister() digitalwrite(latchpin, LOW); shiftout(datapin, clockpin, LSBFIRST, leds); digitalwrite(latchpin, HIGH); void setbrightness(byte brightness) // 0 to 255 analogwrite(outputenablepin, 255-brightness); Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds Page 16 of 17

Other Things to Do Once you know how to to use the 74HC595 then there are many things you can do with lots of LEDs. You could try the following: Make a 'Larson Scanner' scanning lights as found on the front of KITT in the TV series Knight Rider (http://adafru.it/atf) and also found on Cylons in the series Battle Start Galactica. (http://adafru.it/atg) Make an electronic dice. Arrange 6 of the LEDs as two columns of three LEDs with one LED in the middle. Hint take at a look at the Arduino function called 'random'. One of the great things about the 74HC595 is that you can daisy-chain them to increase the number of LEDs that you can control. For this and some other pointers for more advanced use of the 74HC595, take a look at the official Arduino documentation for using 'shiftout' http://arduino.cc/en/tutorial/shiftout (http://adafru.it/ath) You are going to use much of the same breadboard setup in lesson 5 with a few minor changes, so do not dismantle it all just yet. Click Here for the Next Lesson http://adafru.it/auy About the Author Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit: Programming Arduino (http://adafru.it/1019), 30 Arduino Projects for the Evil Genius (http://adafru.it/868) and Programming the Raspberry Pi (http://adafru.it/am5). Adafruit Industries Last Updated: 2014-09-01 11:30:11 AM EDT Page 17 of 17