Search code examples
arduinosensorsdisplaysegment

I have an a 4 digit 7 segment display project with an Arduino Nano, but it doesn't work


The project is about displaying the temperature on the display.

Circuit contains the following:

  • Arduino Nano(AtMega328p)
  • 4 digit 7-segment display(Kingbright CA56-12SRWA)
  • 74HC595N shift register
  • 4x 220ohm resistors for the display
  • DHT11 temperature and humidity sensor
  • 1k ohm resistor for the sensor

The program:

#include "DHT.h"

#define DHTPIN 7
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

const int latchPin = 5;
const int dataPin = 6;
const int clockPin = 4;

const int digitPins[4] = 
{
    A0, A3, A2, A1  
};

const byte digit[10] =
{
    B01010000, //0
    B11010111, //1
    B01100100, //2
    B01000110, //3
    B11000011, //4
    B01001010, //5
    B01001000, //6
    B11010110, //7
    B01000000, //8
    B01000010 //9
};

int digitBuffer[4] = {0};

int digitScan = 0;

void updateDisp(){
   for(byte j = 0; j < 4; j++){
      digitalWrite(digitPins[j], LOW);
   }

   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, B11111111);
   digitalWrite(latchPin, HIGH);

   delayMicroseconds(100);
   digitalWrite(digitPins[digitScan], HIGH);

   digitalWrite(latchPin, LOW);
   if(digitScan==1){
      shiftOut(dataPin, clockPin, MSBFIRST, ~(digit[digitBuffer[digitScan]] | 
B01000000));
   }
   else
   {
      shiftOut(dataPin, clockPin, MSBFIRST, ~(digit[digitBuffer[digitScan]]));
   }

   digitalWrite(latchPin, HIGH);
   digitScan++;
   if(digitScan > 3){digitScan = 0;}
}

void setup() {
   Serial.begin(9600);
   pinMode(latchPin, OUTPUT);
   pinMode(dataPin, OUTPUT);
   pinMode(clockPin, OUTPUT);

   for(int i = 0; i < 4; i++){
      pinMode(digitPins[i], OUTPUT);
   }

   dht.begin();
}

void loop() {

   float h = dht.readHumidity();
   float t = dht.readTemperature();
   float hi = dht.computeHeatIndex(t, h, false);

   digitBuffer[3] = int(t)/1000;
   digitBuffer[2] = (int(t)%1000)/100;
   digitBuffer[0] = (int(t)%100)/10;
   digitBuffer[1] = (int(t)%100)%10;
   updateDisp();
   Serial.println(t);
   delay(5);
}

The problem, is that the leds don't show numbers, but I tested the binary and in the test program all digits worked. Also nearly every 2 seconds all leds turn of f and then turn on again. In theory there could be no problems with the components, nor with the connections. Please don't be angry if I made a huge mistake somewhere, I'm a beginner. I'm more interested in the circuit part than the coding, I couldn't solve the problem.


Solution

  • I believe you have used the code from here
    https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino

    You system is updating the display too fast, as such the DHT is not able to process the temperature correctly, and ends up giving garbage values.

    Increase the delay from 5 (which is 5ms) to about 250 (to 250 ms), you will see digits. I would suggest using 2000ms, but I feel 250 should work.