Hi,
I am doing a binary counter project involving 74HC590 and Arduino Uno. The counter part works fine, only the output of the counter is a bit lower at 3.5V for high level. So I put a voltage level shifter for each bit, so that the Arduino would read either strong 5V or 0V. The only problem is that it does not... (wiring circuit is down bellow.) The voltage level shifter is an inverter, so in the code I invert the bits once again to get the real 74HC590 output. My digitalRead always return logic high, even when the voltage at that point is zero volts.
I have been checking my wiring, looking for floating or shorted or not connected pins and playing with the multimeter all day. From the hardware perspective, my circuit is flawless.
The problem is that no matter what the 74HC590 outputs and what the 2N2222 outputs as well, the reading of digitalRead is always high (before the software inversion of bits.) I can clearly see that my multimeter shows strong 0V at the collector of the 2N2222 which is directly connected to the Uno, but digitalRead answer stays high. If I physically unplug the wire and plug it straight to ground, then the answer becomes correct. This is literally driving me crazy, there is absolutely nothing between the emitter and ground. Why is that I always read logic high at the collector, even if the real voltage measured by multimeter is 0V?
You dont have have examine my code line by line, all the magic happens in the Timer_up() function.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
unsigned long Start = 0;
unsigned long Initial_value = 7000;
volatile bool button_pressed = false;
bool Timer_on = false;
int Measuring_time;
//ISEJIMAI------------------------------------------------------------
const int Decoder_A0 = 10;
const int Decoder_A1 = 19;
const int Decoder_A2 = 12;
const int PWM = 11;
const int Timer_1_16_32_STOP = 18;
//IEJIMAI------------------------------------------------------------
const int button = 13;
const int Time_block_1 = 14;
const int Time_block_2 = 15;
const int Time_block_3 = 16;
const int Time_block_4 = 17;
const int BIT_1 = 2; //LSB
const int BIT_2 = 3;
const int BIT_3 = 4;
const int BIT_4 = 5;
const int BIT_5 = 6;
const int BIT_6 = 7;
const int BIT_7 = 8;
const int BIT_8 = 9; //MSB
//-----------------------------------------------------------------
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.print("Hi");
Serial.print("Hello world.");
pinMode(Decoder_A0, OUTPUT);
pinMode(Decoder_A1, OUTPUT);
pinMode(Decoder_A2, OUTPUT);
pinMode(PWM, OUTPUT);
digitalWrite(PWM, LOW);
pinMode(Timer_1_16_32_STOP, OUTPUT);
digitalWrite(Decoder_A0, LOW);
digitalWrite(Decoder_A1, LOW);
digitalWrite(Decoder_A2, LOW);
digitalWrite(Timer_1_16_32_STOP, LOW);
//------------------------------
pinMode(button,INPUT);
pinMode(Time_block_1,INPUT);
pinMode(Time_block_2,INPUT);
pinMode(Time_block_3,INPUT);
pinMode(Time_block_4,INPUT);
pinMode(BIT_1,INPUT);
pinMode(BIT_2,INPUT);
pinMode(BIT_3,INPUT);
pinMode(BIT_4,INPUT);
pinMode(BIT_5,INPUT);
pinMode(BIT_6,INPUT);
pinMode(BIT_7,INPUT);
pinMode(BIT_8,INPUT);
}
void loop() {
if (Timer_on && (millis() - Start >= Measuring_time)) {
Timer_on = false;
Timer_up();
}
if (digitalRead(button)){
lcd.clear();
lcd.print("Counting...");
Serial.println("Counting...");
Measuring_time=(((digitalRead((Time_block_1))*8)+(digitalRead(Time_block_2)*4)+(digitalRead(Time_block_3)*2)+(digitalRead(Time_block_4)*1))*Initial_value)+Initial_value;
Start = millis();
Timer_on = true;
button_pressed = false;
}
//Dummy signal for the 74HC590 counter to count something
digitalWrite(PWM, HIGH);
delay(200);
digitalWrite(PWM, LOW);
delay(200);
digitalWrite(PWM, HIGH);
delay(200);
digitalWrite(PWM, LOW);
delay(200);
digitalWrite(PWM, HIGH);
delay(200);
digitalWrite(PWM, LOW);
delay(200);
digitalWrite(PWM, HIGH);
delay(200);
digitalWrite(PWM, LOW);
delay(200);
}
//----------------------------------
void Timer_up() {
uint32_t Rezultatas = 0;
byte MSB = 0b0;
byte UPPER8 = 0b0;
byte LOWER8 = 0b0;
byte LSB = 0b0;
delay(1);
digitalWrite(Decoder_A0, LOW); //Selecting the 74HC590 chip, these 3 outputs work like a chip select in SPI or similar
delay(1);
digitalWrite(Decoder_A1, LOW);
delay(1);
digitalWrite(Decoder_A2, LOW);
delay(1000);
digitalWrite(Timer_1_16_32_STOP, HIGH); //Trigger for the 74HC590 to capture the most recent counter value to the registers and hold it
delay(1000); //Extra long delays to be sure that none of the 74HC590 internal timings are violated
LSB |= !digitalRead(BIT_1);
Serial.println(digitalRead(BIT_1));
LSB |=((!digitalRead(BIT_2)) << 1);
Serial.println(!digitalRead(BIT_2));
LSB |=((!digitalRead(BIT_3)) << 2);
Serial.println(!digitalRead(BIT_3));
LSB |=((!digitalRead(BIT_4)) << 3);
Serial.println(!digitalRead(BIT_4));
LSB |=((!digitalRead(BIT_5)) << 4);
Serial.println(!digitalRead(BIT_5));
LSB |=((!digitalRead(BIT_6)) << 5);
Serial.println(!digitalRead(BIT_6));
LSB |=((!digitalRead(BIT_7)) << 6);
Serial.println(!digitalRead(BIT_7));
LSB |=((!digitalRead(BIT_8)) << 7);
Serial.println(!digitalRead(BIT_8));
Serial.println(" ");
lcd.clear();
lcd.print(LSB);
Serial.println(LSB);
}