Hello all,
This is a continuation post of a previous question in the Arduino Zero sub-forum (https://forum.arduino.cc/index.php?topic=420734.0). I am posting here because I have realised my problem is electronics and does not relate to Zero.
I have had problems with erratic circuit behaviour and after it was suggested to me that decoupling capacitors be added, I initially had no success. However with more careful testing I have found that they are definitely helping - but the result is not yet perfect. I therefore think that the problem could be to do with selection of correct capacitances, and possibly placement as well.
The circuit is an array of 8 shift registers (74HC165) being used to read 64 digital inputs. I am using a Due but have had similar problems with a Zero.
Here is a diagram of my circuit and the code used to read the inputs.
/*
COMMENTS
*/
#include <math.h>
#define STRLEN 25
//dont use pins 0 or 1 it screws upload
#define ledCLKPin 2 // aka CLK
#define ledCSPin 3 // aka LOAD
#define ledDINPin 4 //MAX7219 Pin 1
#define cPin 26 // CP (26)
#define ePin 24 // CE (24)
#define pPin 22 // PL (22)
#define outputPin 28 // (28)
#define stepDelay 2
#include "LedControl.h" // need the library
LedControl lc = LedControl(ledDINPin, ledCLKPin, ledCSPin, 1); //
// 1 as we are only using 1 MAX7219
char buffer[STRLEN];
int bufferIndex = 0;
int readings[8][8]; // [0][0] is a1, [0][7] is a8, [7][0] is h1, [7][7] is h8
float voltages[8][8];
void setup()
{
int intCounter = 0;
Serial.begin(9600);
pinMode(ledDINPin, OUTPUT);
pinMode(ledCLKPin, OUTPUT);
pinMode(ledCSPin, OUTPUT);
pinMode(cPin, OUTPUT);
pinMode(pPin, OUTPUT);
pinMode(ePin, OUTPUT);
pinMode(outputPin, INPUT);
defaultSettings();
//led setup
// the zero refers to the MAX7219 number, it is zero for 1 chip
lc.shutdown(0, false); // turn off power saving, enables display
lc.setIntensity(0, 1); // sets brightness (0~15 possible values)
lc.clearDisplay(0);// clear screen
Serial.println("Completed setup().");
}
void defaultSettings()
{
digitalWrite(cPin, LOW);
digitalWrite(pPin, HIGH);
digitalWrite(ePin, HIGH);
}
void loop()
{
readAll();
printArray(); //slows it down a lot!
updateLED();
//delay(scanDelay); //was delay 2
Serial.println("Completed loop().");
}
void readAll()
{
defaultSettings();
delay(stepDelay);
digitalWrite(pPin, LOW); delay(stepDelay); digitalWrite(pPin, HIGH); //refresh readings .. way delay 100
delay(stepDelay);
digitalWrite(ePin, LOW); //enable clock
delay(stepDelay);
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++) //order of readings given is A1..A8..H1..H8
{
digitalWrite(cPin, LOW);
delay(stepDelay); //temp
readings[i][j] = digitalRead(outputPin); //perhaps should reverse in order because chip outputs in reverse
//voltages[i][j] = analogRead(A0) * (3.3 / 1023.0);
digitalWrite(cPin, HIGH);
delay(stepDelay); //was delay 10
}
}
}
void printArray()
{
Serial.println("Array values: ");
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Serial.print(readings[i][j]);
}
Serial.print(" ");
for (int j = 0; j < 8; j++)
{
Serial.print(voltages[i][j]);
Serial.print(" ");
}
Serial.println();
}
}
void updateLED()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
lc.setLed(0, i, j, readings[i][j]);
//delay(25);
}
}
}
My problem is that the readings start to go awry the more switches are closed i.e. inputs into the shift registers reading HIGH. Before I added any capacitors this started to happen when more than 10 inputs were HIGH, now it’s stable up to about 45 but still starts to go wrong as I approach 64. The capacitors I have chosen so far are based on various tutorial examples so I would have thought they would be roughly right. I have cut the legs short and placed the IC capacitors right onto the Vcc pins of the 74HC165 chips.
Currently I am using capacitances as follows:
- 10µF 16V Tantalum Bead Capacitor for Caps A and B
- 0.1µF ceramic capacitor for Caps C1-8
My questions are:
- Is the behaviour I am seeing (i.e. problem getting worse with increasing number of HIGH inputs) consistent with the decoupling capacitors being insufficient?
- Does the max7219-driven LED array need decoupling (labelled as Cap A) and could that be affecting the shift registers even though it is on the 5v supply and the shift register circuit is on 3.3v?
- What capacitances should I be trying for caps A, B and C1-8 to get it to start working more reliably?
As an aside, is there any reason why my trusty old Duemilanove continues to work perfectly without any decoupling capacitors while my Zero and Due need them?
Really appreciate any help anyone is able to provide.
Thanks,
Chris