{"id":316,"date":"2013-01-26T13:39:00","date_gmt":"2013-01-26T13:39:00","guid":{"rendered":"http:\/\/thomas.w-p.me.uk\/blog\/2013\/01\/quad-seven-segment-display-with-shift-register\/"},"modified":"2013-01-26T13:39:00","modified_gmt":"2013-01-26T13:39:00","slug":"quad-seven-segment-display-with-shift-register","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2013\/01\/quad-seven-segment-display-with-shift-register\/","title":{"rendered":"Quad seven segment display with shift register"},"content":{"rendered":"<p>I wanted to control a seven segment display but still have enough pins left to work my ultrasonic distance sensor.\u00a0 The next step was to make my quad display work.\u00a0\u00a0 I carefully worked through what each pin did using a spreadsheet and found I could display any number I liked at any one time, but they would all show the same number.\u00a0 Clearly the display needed to be refreshed in sequence somehow.\u00a0 My eureka moment was realising that this is exactly how it works &#8211; you just switch them on and off, one after the other really really fast.\u00a0 The eye can&#8217;t tell this is happening so you see it all as one display.\u00a0 PENNY D&#8230;.R&#8230;.O&#8230;.P&#8230;.S. <\/p>\n<p>I found <a href=\"http:\/\/www.pial.net\/post\/Arduino-controlling-a-4-digit-seven-segment-display.aspx\">Pial&#8217;s Blog<\/a> about using a shift register to control a 4 segment display and after much fiddling got it to work once I realised that his display worked on common anode (having a voltage made it work) whereas mine was common cathode (no voltage makes things work).\u00a0 I realised I had to make everything down where his was up and up where his was down.\u00a0 I then got a beautiful negative display of the numbers (the segments I wanted off were on and visa versa) which confused me for a while.\u00a0 I&#8217;d spotted a weird tilde ~ on the start of some variables and after a little research found that this made the binary numbers the &#8220;negative&#8221; (01000100 becoming 10111011).\u00a0 I removed the ~ and bingo, I could control the numbers.\u00a0 I found a minor bug in his digits too &#8211; his 5 made a 3 so I fixed that. <\/p>\n<div><a href=\"http:\/\/2.bp.blogspot.com\/-M1yB6Q77mqg\/UQPXbqPq9AI\/AAAAAAAASVo\/rOEQF4asS_w\/s1600\/2013-01-26+12.43.42.jpg\" imageanchor=\"1\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" height=\"480\" src=\"http:\/\/2.bp.blogspot.com\/-M1yB6Q77mqg\/UQPXbqPq9AI\/AAAAAAAASVo\/rOEQF4asS_w\/s640\/2013-01-26+12.43.42.jpg\" width=\"640\"><\/a><\/div>\n<p>This code just prints random numbers at a fast flicker to prove I can change things on the fly.<br \/><span><span><!--4--><!--4--><\/span><\/span><\/p>\n<p><span><span>\/*<br \/>\u00a0 Shift Register Example<br \/>\u00a0for 74HC595 shift register<\/p>\n<p>\u00a0This sketch turns reads serial input and uses it to set the pins<br \/>\u00a0of a 74HC595 shift register.<\/p>\n<p>\u00a0Hardware:<br \/>\u00a0* 74HC595 shift register attached to pins 2, 3, and 4 of the Arduino,<br \/>\u00a0as detailed below.<br \/>\u00a0* LEDs attached to each of the outputs of the shift register<\/p>\n<p>\u00a0Created 22 May 2009<br \/>\u00a0Created 23 Mar 2010<br \/>\u00a0by Tom Igoe<\/p>\n<p>\u00a0*\/<\/p>\n<p>\/\/Pin connected to latch pin (ST_CP) of 74HC595<br \/>const int latchPin = 8;<br \/>\/\/Pin connected to clock pin (SH_CP) of 74HC595<br \/>const int clockPin = 12;<br \/>\/\/\/\/Pin connected to Data in (DS) of 74HC595<br \/>const int dataPin = 11;<\/p>\n<p>\/\/http:\/\/www.pial.net\/post\/Arduino-controlling-a-4-digit-seven-segment-display.aspx<br \/>const int digitPins[4] = {2,3,4,5}; \/\/pins to control the 4 common anode pins of the display<br \/>const byte digit[10] = \/\/seven segment digit bits<br \/>{<br \/>\u00a0 B00111111, \/\/0<br \/>\u00a0 B00000110, \/\/1<br \/>\u00a0 B01011011, \/\/2<br \/>\u00a0 B01001111, \/\/3<br \/>\u00a0 B01100110, \/\/4<br \/>\u00a0 B01101101, \/\/5<br \/>\u00a0 B01111101, \/\/6<br \/>\u00a0 B00000111, \/\/7<br \/>\u00a0 B01111111, \/\/8<br \/>\u00a0 B01101111\u00a0 \/\/9<br \/>};<br \/>int digitScan = 0; \/\/to interate through the digits in the display<br \/>int digitBuffer[4] = {0}; \/\/ to?<\/p>\n<p>void setup() {<br \/>\u00a0 \/\/set pins to output because they are addressed in the main loop<br \/>\u00a0 pinMode(latchPin, OUTPUT);<br \/>\u00a0 pinMode(dataPin, OUTPUT);\u00a0 <br \/>\u00a0 pinMode(clockPin, OUTPUT);<br \/>\u00a0 <br \/>\u00a0 for(int i=0;i < 4 br=\"\" gt=\"\" i=\"\" nbsp=\"\">\u00a0\u00a0\u00a0 pinMode(digitPins[i],OUTPUT);<br \/>\u00a0 }<br \/>\u00a0 <\/p>\n<p>}<\/p>\n<p>void loop() {<br \/>\u00a0 <br \/>\u00a0 \/\/if (Serial.available() > 0) {<br \/>\u00a0\u00a0\u00a0 \/\/ ASCII &#8216;0&#8217; through &#8216;9&#8217; characters are<br \/>\u00a0\u00a0\u00a0 \/\/ represented by the values 48 through 57.<br \/>\u00a0\u00a0\u00a0 \/\/ so if the user types a number from 0 through 9 in ASCII, <br \/>\u00a0\u00a0\u00a0 \/\/ you can subtract 48 to get the actual value:<br \/>\u00a0\u00a0\u00a0 \/\/int bitsToSend = Serial.parseInt();<br \/>\u00a0\u00a0\u00a0 \/\/Serial.println(&#8220;Wrote: &#8221; + String(bitsToSend));<br \/>\u00a0\u00a0\u00a0 <br \/>\u00a0\u00a0\u00a0 \/\/drop the latch<br \/>\u00a0\u00a0\u00a0 \/\/digitalWrite(latchPin, LOW);<br \/>\u00a0\u00a0\u00a0 <br \/>\u00a0\u00a0\u00a0 \/\/ shift the bits out:<br \/>\u00a0\u00a0\u00a0 \/\/shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);<br \/>\u00a0 <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ turn on the output so the LEDs can light up:<br \/>\u00a0\u00a0\u00a0 \/\/digitalWrite(latchPin, HIGH);<br \/>\u00a0 \/\/}<br \/>\u00a0 \/\/ wait for a bit<br \/>\u00a0 \/\/delayMicroseconds(400);<br \/>\u00a0 delay(1000);<br \/>\u00a0 refreshDisplay();<br \/>}<\/p>\n<p>\/\/ Going to change the display<br \/>void refreshDisplay()<br \/>{<br \/>\u00a0 \/\/ make all the display segments LOW<br \/>\u00a0 for(byte k=0;k<span> < <\/span>4 br=&#8221;&#8221; k=&#8221;&#8221;>\u00a0 {<br \/>\u00a0\u00a0\u00a0 digitalWrite(digitPins[k], HIGH);<br \/>\u00a0 }<\/p>\n<p>\u00a0 \/\/ the numbers I am outputting, need to link to sensor next <br \/>\u00a0\u00a0\u00a0 digitBuffer[0] = random(0, 9);<br \/>\u00a0\u00a0\u00a0 digitBuffer[1] = random(0, 9);<br \/>\u00a0\u00a0\u00a0 digitBuffer[2] = random(0, 9);<br \/>\u00a0\u00a0\u00a0 digitBuffer[3] = random(0, 9);<br \/>\u00a0 \/\/ open the latch<br \/>\u00a0 digitalWrite(latchPin, LOW);\u00a0 <br \/>\u00a0 <br \/>\u00a0 \/\/ clear all the pins<br \/>\u00a0 shiftOut(dataPin, clockPin, MSBFIRST, B00000000);<br \/>\u00a0 <br \/>\u00a0 \/\/ close the latch pin<br \/>\u00a0 digitalWrite(latchPin, HIGH);<br \/>\u00a0 <br \/>\u00a0 \/\/ wait for a bit<br \/>\u00a0 delayMicroseconds(400);<br \/>\u00a0 <br \/>\u00a0 \/\/ makes the right digit high<br \/>\u00a0 digitalWrite(digitPins[digitScan], LOW); <\/p>\n<p>\u00a0 \/\/ open the latch<br \/>\u00a0 digitalWrite(latchPin, LOW);\u00a0 <br \/>\u00a0 if(digitScan==0) \/\/ 0 is the first character &#8211; I want 3.123m<br \/>\u00a0 {<br \/>\u00a0\u00a0\u00a0 \/\/ write the number with the dp<br \/>\u00a0\u00a0\u00a0 shiftOut(dataPin, clockPin, MSBFIRST, (digit[digitBuffer[digitScan]] | B10000000)); \/\/inserting the dot<br \/>\u00a0 }<br \/>\u00a0 else<br \/>\u00a0 {<br \/>\u00a0\u00a0\u00a0 \/\/ write the number<br \/>\u00a0\u00a0\u00a0 shiftOut(dataPin, clockPin, MSBFIRST, digit[digitBuffer[digitScan]]);<br \/>\u00a0 }<br \/>\u00a0 digitalWrite(latchPin, HIGH);<br \/>\u00a0 digitScan++;<br \/>\u00a0 if(digitScan>3) digitScan=0;<br \/>\u00a0 <br \/>}<!--4--><\/span><\/span><\/p>\n<p><span><span><!--4--><!--4--><\/span><\/span><br \/>Next step is to link up to the distance sensor, but I now need to make a bug hotel with youngest.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to control a seven segment display but still have enough pins left to work my ultrasonic distance sensor.\u00a0 The next step was to make my quad display work.\u00a0\u00a0 I carefully worked through what each pin did using a spreadsheet and found I could display any number I liked at any one time, but &hellip; <a href=\"https:\/\/thomas.w-p.me.uk\/blog\/2013\/01\/quad-seven-segment-display-with-shift-register\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Quad seven segment display with shift register<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[41,42,43],"class_list":["post-316","post","type-post","status-publish","format-standard","hentry","tag-arduino","tag-quad-seven-segment-display","tag-shift-register"],"_links":{"self":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/comments?post=316"}],"version-history":[{"count":0,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/316\/revisions"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}