I'm trying to control a 4-digit 7 segment display with an Arduino Nano, but I'm getting something wrong. I'm using the ICSP header to interface with a 74HC595 Shift Register which is then connected to the display. I've tested and retested the connections and everything seems to be alright, however the shift register doesn't have the desired output, and I notice it only shifs one single bit when I press the reset button on the arduino board.
74HC595 | A. Nano
DS <-----> MOSI (ICSP)
SH_CP <-----> SCK (ICSP)
ST_CP <-----> D6
MR <-----> 5V (ICSP)
OE <-----> GND (ICSP)
VDD <-----> 5V (ICSP)
GND <-----> GND (ICSP)
My code:
//pin connected to STCP
const int latchPin = 6;
//pin connected to SHCP
const int clockPin = 17;
//pin connected to DS (MOSI)
const int dataPin = 11;
byte val=0;
void setup() {
// put your setup code here, to run once:
//SPI pins are output
pinMode(latchPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(dataPin,OUTPUT);
pinMode(3,OUTPUT);
updateRegister(0);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(3,HIGH);
for (int i=0; i<8; i++) {
updateRegister();
delay(500);
}
}
void updateRegister() {
//pull latch low to write new bits to the register
digitalWrite(latchPin,LOW);
//shift out the data
shiftOut(dataPin,clockPin,LSBFIRST,val);
//pull latch pin high so that the leds light up
digitalWrite(latchPin,HIGH);
}
-
This site has a question and answer format. What is your question, exactly? (Note that questions generally end with a question mark). Please read How do I ask a good question?Nick Gammon– Nick Gammon ♦2016年01月26日 07:10:24 +00:00Commented Jan 26, 2016 at 7:10
-
Please post a schematic of your wiring. You say in the question that you are using the ICSP header, but then the code uses pins 6 and 17 which are not ICSP (or SPI) pins.Nick Gammon– Nick Gammon ♦2016年01月26日 20:13:23 +00:00Commented Jan 26, 2016 at 20:13
3 Answers 3
const int clockPin = 17;
I'm pretty sure that should be 13, not 17.
-
Yup, this was my mistake. christianto.tjahyadi.com/wp-content/uploads/2014/11/nano.jpg this image mislead me to believe SPI clock was at pin 17. With pin 13, the display is operating as it should.joaocandre– joaocandre2016年01月27日 14:36:57 +00:00Commented Jan 27, 2016 at 14:36
It is extremely confusing to have a variable val
and then use _val
in your function but then:
shiftOut(dataPin,clockPin,LSBFIRST,val);
As James pointed out, you are not sending the value passed to the function, to the shift register.
Also this is just going to give you a boolean:
updateRegister(!i);
Why take "not i" there?
I suggest you use SPI and not muck around with shiftOut. I have a page about the 595 chip including example code. eg.
#include <SPI.h>
const byte LATCH = 10;
void setup ()
{
SPI.begin ();
} // end of setup
byte c;
void loop ()
{
c++;
digitalWrite (LATCH, LOW);
SPI.transfer (c);
digitalWrite (LATCH, HIGH);
delay (20);
} // end of loop
-
I posted the code with some errors (I was paying with the varaibles trying to solve the problem). As it was it was bovisously not going to work, but that was not the cause of the problem. I tried that snippet with the SPI library, but the display/leds do not light up at all. Also, it just crossed my mind - could the problem be caused by calling shiftOut() on SPI pins?joaocandre– joaocandre2016年01月26日 03:59:48 +00:00Commented Jan 26, 2016 at 3:59
-
What LEDs exactly?2016年01月26日 04:31:05 +00:00Commented Jan 26, 2016 at 4:31
-
could the problem be caused by calling shiftOut() on SPI pins?
- No. If you don't callSPI.begin()
then SPI is inactive.2016年01月26日 07:13:46 +00:00Commented Jan 26, 2016 at 7:13 -
The leds on the 7-segment displayjoaocandre– joaocandre2016年01月26日 10:58:01 +00:00Commented Jan 26, 2016 at 10:58
Do you need to add the _ to the shiftOut(dataPin,clockPin,LSBFIRST,val); ?
-
James, you were right about the underscore, but the Stack Exchange system automatically flags very short answers. If you expand it out a bit more you are protected from this automated flagging. Welcome to Arduino Stack Exchange. :)2016年01月26日 00:23:13 +00:00Commented Jan 26, 2016 at 0:23
-
It was a mistake, I was tinkering with the code. I've updated the question, but I can say that was not the source of the problem.joaocandre– joaocandre2016年01月26日 03:57:22 +00:00Commented Jan 26, 2016 at 3:57
-
Your updated code sends 0 to the shift register, so I'm not too surprised nothing lights up.2016年01月26日 07:04:59 +00:00Commented Jan 26, 2016 at 7:04
-
The 7-segment display has a common anode, sending a '0' to the shift register should light up all the segments (an '8'), since I'm pulling the anode up with 'digitalWrite(3,HIGH); 'joaocandre– joaocandre2016年01月26日 11:00:41 +00:00Commented Jan 26, 2016 at 11:00
Explore related questions
See similar questions with these tags.