element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum 4 Digit 7 Segment Thermometer Help
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 10 replies
  • Subscribers 394 subscribers
  • Views 1259 views
  • Users 0 members are here
Related

4 Digit 7 Segment Thermometer Help

Former Member
Former Member over 10 years ago

Hi Guys,

 

So I decided I was going to start learning how to use 7 Segment Displays and shift registers. I am using a 74HC595 to control the anodes of a 4 digit 7 segment display. And 2N3904s for the Cathodes. and a Dallas DS18B20 temperature sensor.

 

However, I am having extreme flicker/timing issues with the display. here is the code I am using. Instead of the display normally updating it cycles through each Digit with a about one second delay before displaying the next digit. So it displays one digit at a time. I have read that DallasTemp library causes delays. But the display shouldn't have large delays in between numbers? So I am slightly confused. Can anyone help me out?

 

Thanks,

Austin

 

/*
* created by Rui Santos, http://randomnerdtutorials.com
* Temperature Sensor Displayed on 4 Digit 7 segment common anode
* 2013
*
* Modified by Austin Pauley for use with DS18B20
* 2015
*/


#include <OneWire.h>
#include <DallasTemperature.h>


#define ONE_WIRE_BUS A0
OneWire ds(ONE_WIRE_BUS);
DallasTemperature sensors(&ds);
DeviceAddress insideThermometer;


const int digitPins[4] = {
  5,4,3,2};                 //4 common anode pins of the display
const int clockPin = 11;    //74HC595 Pin 11 
const int latchPin = 12;    //74HC595 Pin 12
const int dataPin = 13;     //74HC595 Pin 14
const byte digit[10] =      //seven segment digits in bits
{
  B11000000, //0
  B11111001, //4
  B10100100, //2
  B10110000, //3
  B10011001, //4
  B10010010, //5
  B10000010, //6
  B11111000, //7
  B10000000, //8
  B10010000  //9
};
int digitBuffer[4] = {
  0,0,0,0};
int digitScan = 0, flag = 0, soft_scaler = 0;
;
float  tempC, tempF;


void setup(){
  for (int i = 0; i<4; i++)
  {
  pinMode(digitPins[i], OUTPUT);
  }
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  sensors.begin();
  sensors.getAddress(insideThermometer, 0);
  Serial.begin(9600);
  sensors.setResolution(9);

}




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);


  shiftOut(dataPin, clockPin, MSBFIRST, digit[digitBuffer[digitScan]]);


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



}


float getTemp()
{
sensors.requestTemperatures();
tempF = sensors.getTempF(insideThermometer);
}
void loop(){
  int temp = (int)getTemp();


  digitBuffer[3] = int(temp) / 1000;
  digitBuffer[2] = (int(temp) % 1000) / 100;
  digitBuffer[1] = (int(temp) % 100) / 10;
  digitBuffer[0] = (int(temp) % 100) % 10;
  updateDisp();

}

  • Sign in to reply
  • Cancel

Top Replies

  • sftwrngnr
    sftwrngnr over 10 years ago +1
    First off, I would only update the display if the temperature has actually changed, otherwise, you're reloading and cycling the shift register constantly. You could simply retain the previous displayed…
  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago in reply to Former Member +1
    Thats a good start but the call to requestTemperatures is a blocking call and it does several things including reseting the one wire bus, initiating a start conversion on all connected devices and then…
  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago in reply to sftwrngnr +1
    one wire in itself is not blocking, you can initiate the conversion, go away to do something else (Like update a 7Segment display) and come back later to see if the conversion is ready to be read. the…
  • sftwrngnr
    0 sftwrngnr over 10 years ago

    First off, I would only update the display if the temperature has actually changed, otherwise, you're reloading and cycling the shift register constantly.  You could simply retain the previous displayed temperature in a buffer, and then only shift in and modify the digits that need to be changed.

    Secondly, you mention that it's slow.  You're delaying 100 microseconds between writes to the display, so that is likely the culprit.

     

    -Dan

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to sftwrngnr

    I have tried what you stated, and it is noticeably faster, but I can still see it "scanning" the digits. I know that I shouldn't but cannot find out why?

     

    /*
    * created by Rui Santos, http://randomnerdtutorials.com
    * Temperature Sensor Displayed on 4 Digit 7 segment common anode
    * 2013
    *
    * Modified by Austin Pauley for use with DS18B20
    * 2015
    */
    
    
    #include <OneWire.h>
    #include <DallasTemperature.h>
    
    
    #define ONE_WIRE_BUS A0
    OneWire ds(ONE_WIRE_BUS);
    DallasTemperature sensors(&ds);
    DeviceAddress insideThermometer;
    
    
    const int digitPins[4] = {
      5,4,3,2};                 //4 common anode pins of the display
    const int clockPin = 11;    //74HC595 Pin 11 
    const int latchPin = 12;    //74HC595 Pin 12
    const int dataPin = 13;     //74HC595 Pin 14
    const byte digit[10] =      //seven segment digits in bits
    {
      B11000000, //0
      B11111001, //4
      B10100100, //2
      B10110000, //3
      B10011001, //4
      B10010010, //5
      B10000010, //6
      B11111000, //7
      B10000000, //8
      B10010000  //9
    };
    int digitBuffer[4] = {
      0,0,0,0};
    int digitScan = 0, pastTemp = 0, currentTemp = 0;
    ;
    float  tempC, tempF;
    
    
    float getTemp()
    {
      sensors.requestTemperatures();
      tempF = sensors.getTempF(insideThermometer);
    }
    
    
    void setup(){
      for (int i = 0; i<4; i++)
      {
      pinMode(digitPins[i], OUTPUT);
      }
      pinMode(latchPin, OUTPUT);
      pinMode(clockPin, OUTPUT);
      pinMode(dataPin, OUTPUT);
      sensors.begin();
      sensors.getAddress(insideThermometer, 0);
      Serial.begin(9600);
      sensors.setResolution(9);
      currentTemp = (int)getTemp();
    }
    
    
    
    
    void updateDisp(){
      for (byte j = 0; j < 4; j++)
      digitalWrite(digitPins[j], LOW);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, B11111111);
      digitalWrite(latchPin, HIGH);
    
    
      digitalWrite(digitPins[digitScan], HIGH);
    
    
      digitalWrite(latchPin, LOW);
    
    
      shiftOut(dataPin, clockPin, MSBFIRST, digit[digitBuffer[digitScan]]);
    
    
      digitalWrite(latchPin, HIGH);
      digitScan++;
      if (digitScan>3) digitScan = 0;
    
    
    
    }
    
    
    
    
    void loop(){
    
      Serial.println(currentTemp);
      currentTemp = (int)getTemp();
      if (pastTemp != currentTemp)
      {
      digitBuffer[0] = int(currentTemp) / 1000;
      digitBuffer[1] = (int(currentTemp) % 1000) / 100;
      digitBuffer[2] = (int(currentTemp) % 100) / 10;
      digitBuffer[3] = (int(currentTemp) % 100) % 10;
      pastTemp = currentTemp;
      }
      updateDisp();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }

    Is this what you wanted me to do?

    -Austin

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to Former Member

    Thats a good start but the call to requestTemperatures is a blocking call and it does several things including reseting the one wire bus, initiating a start conversion on all connected devices and then waits for all conversions to complete, this can be quite a significant delay

     

    A way around this is to set waitForConversion to false with a call to setWaitForConversion but you will have to repeatedly check isConversionAvailable to see if the conversion has completed

     

    this can be integrated into the gettemp function by testing if conversion is complete first, if it is not then return the last value read, if it is complete then read the temperature into your holding variable and imediatly initiate a new conversion (Even if you dont need it yet as it will take a while to complete) as much as 750 - 1000mS and will be the reason for your scanning display issues as at the end of a scan there will be upto 750mS delay while it makes another reading from the temp sensor

     

    this way your never waiting for the conversion to complete (Which is what your currently doing) which is a slow process on the temp sensors.

    You could improve even more by moving the conversion to your buffer into the gettemp function too and only update the buffer when a conversion is complete

    then the main Loop() simply calls gettemp, then update display, it will never know or care if a new conversion has happened, it will simply display the contents of the buffer[], the conversion to the temperature is also only done after a sucesful conversion

     

    this is a half baked version of this where the sketch simply waits a set amount of time ather than testing for completion https://github.com/milesburton/Arduino-Temperature-Control-Library/blob/master/examples/WaitForConversion2/WaitForConversion2.pde

     

    This example shows the difference in time for each method https://github.com/milesburton/Arduino-Temperature-Control-Library/blob/master/examples/WaitForConversion/WaitForConversion.pde

     

    neither example actually checks for conversion to complete but it is a simple function to implement and the cpp library file has implementation you could use as part of its coding

     

    there are other optimizations you could perform but "Baby Steps" image

     

    Hope this helps

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • sftwrngnr
    0 sftwrngnr over 10 years ago

    Props to Peter. I didn't realize the one wire call was blocking.  Since it is one wire, you can't really wait asynch for an interrupt. Its been a while since I've done anything with one wire, so you may want to have a look at the protocol and see if there's anything you can do to speed things up a bit.  As Peter says, "baby steps." Work on incremental improvements and see what does and doesn't work.

     

    Best of luck.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to sftwrngnr

    one wire in itself is not blocking, you can initiate the conversion, go away to do something else (Like update a 7Segment display) and come back later to see if the conversion is ready to be read. the dallas / ti chip fully supports this. the blocking is just the default behaviour of the library to make the basic use easy for beginners

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 10 years ago in reply to Robert Peter Oakes

    This is the code I use to check the temperature using One wire.

     

    The idea is to note when you send off the temperature read instruction, and then keep checking to see if 750/1000mS has passed.

    If it has then go read it.

     

    This example only sampled every 10 secs (Line 108)

    Line 150 checks to see if it is time to read and Line 185 resets the timer.

     

    /* 
    based on Eric Tsai 
    License: CC-BY-SA, https://creativecommons.org/licenses/by-sa/2.0/ 
    
    Date: 29-09-2014 
    File: eLDERmon_Temp.ino 
    This sketch is for a wired Arduino w/ RFM69 wireless transceiver 
    Sends sensor data (temp) back to gateway.
    See OpenHAB configuration file. 
    
    1) Update encryption string "ENCRYPTKEY" 
    2) 
    */  
      
      
    /* sensor 
    node = 12 
    device ID 
    6 = 1262, 1263 = temperature, humidity 
    
    
    **** connections *****
    Moteino Pin assignments
       Pin D0  Rx
       Pin D1  Tx
       Pin D2  RFM69 D100
       Pin D3  
       Pin D4  
       Pin D5  
       Pin D6  
       Pin D7  Temp sensor DS18B20 data pin with 4k7 external pullup resistor to 3v3
       Pin D8  
       Pin D9  
       Pin D10  RFM69 SS
       Pin D11  RFM69 MOSI
       Pin D12  RFM69 MISO
       Pin D13  RFM69 SCK
       
       Program as an UNO.
    
    */  
      
      
      
    //RFM69  --------------------------------------------------------------------------------------------------  
    #include <RFM69.h>
    #include <RFM69registers.h>  
    #include <SPI.h> 
    #include <OneWire.h>
    
    
    #define NODEID        12    //unique for each node on same network  
    #define NETWORKID     100  //the same on all nodes that talk to each other  
    #define GATEWAYID     1  
    //Match frequency to the hardware version of the radio on your Moteino (uncomment one):  
    #define FREQUENCY   RF69_433MHZ  
    //#define FREQUENCY   RF69_868MHZ  
    //#define FREQUENCY     RF69_915MHZ  
    #define ENCRYPTKEY    "eLDERmon12345678" //exactly the same 16 characters/bytes on all nodes!  
    #define IS_RFM69HW    //uncomment only for RFM69HW! Leave out if you have RFM69W!  
    #define ACK_TIME      30 // max # of ms to wait for an ack  
    #define LED           9  // Moteinos have LEDs on D9  
    #define SERIAL_BAUD   9600  //must be 9600 for GPS, use whatever if no GPS  
      
    boolean debug = 0;  
      
    //struct for wireless data transmission  
    typedef struct {    
      int             nodeID;             //node ID (1xx, 2xx, 3xx);  1xx = basement, 2xx = main floor, 3xx = outside  
      int             deviceID;           //sensor ID (2, 3, 4, 5)  
      unsigned long   var1_usl;           //uptime in ms  
      float           var2_float;         //sensor data?  
      float           var3_float;         //battery condition?  
    } Payload;  
    Payload theData;  
      
    char buff[20];  
    byte sendSize=0;  
    boolean requestACK = false;  
    RFM69 radio;  
      
    //end RFM69 ------------------------------------------  
    
    //inputs
    const int TempSensor = 7;                   // Tempsensor name
      
    // DS18B20 Temperature chip i/o
    OneWire ds(7);                     // TempSensor on pin 7
    byte i;
    byte present = 0;
    byte data[12];
    byte addr[8];
    word TReading;
    byte HighByte, LowByte;
    int SignBit;  
      
      
    //temperature / humidity  =====================================  
    
    float tempValue_previous = 0;
    
    // 6 = 1262, 1263 = temperature, humidity  
      
      
    // timings  
    
    unsigned long last_temperature_time;
    int temperature_interval = 10000;         // 10 seconds
      
      
    void setup()  
    {  
      Serial.begin(9600);          //  setup serial  
      Serial.println("Serial Up");
      pinMode(TempSensor, INPUT);
      delay(500);
      Temp_init();
      
      //RFM69-------------------------------------------  
      radio.initialize(FREQUENCY,NODEID,NETWORKID);  
      #ifdef IS_RFM69HW  
        radio.setHighPower(); //uncomment only for RFM69HW!  
      #endif  
      radio.encrypt(ENCRYPTKEY);  
    //  char buff[50];  
    //  sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);  
    //  Serial.println(buff);  
      theData.nodeID = NODEID;  //this node id should be the same for all devices in this node  
      //end RFM--------------------------------------------  
    
       
      
      //initialize times  
     last_temperature_time = millis();
      
      
      //PIR sensor  
      Serial.println("Starting now");
      delay(500);
    }  
      
    void loop()  
    {  
      unsigned long time_passed = 0;  
    
      //===================================================================  
      //device #6  
      //temperature / humidity  
      
      if (millis() - last_temperature_time > temperature_interval) 
        {
        
        //reset and setup the temp sensor for communicating
            ds.reset();
            ds.select(addr);
            ds.write(0x44,1);        // start a temp reading
            
            delay(1000);             // wait 1 sec for data to be present
            
            present = ds.reset();
            ds.select(addr);    
            ds.write(0xBE);          // Read Scratchpad in the temp sensor
        
          for ( i = 0; i < 9; i++)   // we need 9 bytes
          {
            data[i] = ds.read();     //read the 9 bytes of data for a temperature      
          }
          
          LowByte = data[0];                       // the first byte is the lowest
          HighByte = data[1];                      // second byte is the highest
          TReading = (HighByte << 8) + LowByte;
          SignBit = TReading & 0x8000;             // test most sig bit
          if (SignBit)                             // If its negative
          {
             TReading = (TReading ^ 0xffff) + 1;   // 2's comp 
          }
         
          float t = TReading *0.0625; 
          if (debug)
          {
            Serial.print("temp = ");
            Serial.println(t);
          }
    
            last_temperature_time = millis(); 
    
            // Check if any reads failed and exit early (to try again).  
            if (isnan(t))
            {  
              if (debug) Serial.println("Failed to read from temp sensor!");
              tempValue_previous =0;  
              return;  
            }
           
           if (t > (tempValue_previous + 0.1)|| (t < (tempValue_previous - 0.1)))
           // only send if not the same  
           {
                //send data  
                theData.deviceID = 6;  
                theData.var1_usl = millis();  
                //theData.var2_float = f;    // Fahrenheit 
                theData.var2_float = t;  
                theData.var3_float = 0;  
                radio.sendWithRetry(GATEWAYID, (const void*)(&theData), sizeof(theData));         
                tempValue_previous = t;
                if (debug)
                {
                   Serial.println("SENDING temp");
                   Serial.print("tempValue_previous = ");
                   Serial.println(tempValue_previous);
                }
           }
            
           if (debug)  
            {  
    
                Serial.print("   Temp= ");
                Serial.print(t);
                Serial.println(" C");
                delay(1000);
            }
      
      }
      
      
    } 
    void Temp_init()
    {
      
      if ( !ds.search(addr)) {
        Serial.print("No more addresses.\n");
        ds.reset_search();
        delay(250);
        return;
      }
      
      Serial.print("R=");
      for( i = 0; i < 8; i++) {
        Serial.print(addr[i], HEX);
        Serial.print(" ");
      }
    
      if ( OneWire::crc8( addr, 7) != addr[7]) {
          Serial.print("CRC is not valid!\n");
          return;
      }
      
      if ( addr[0] != 0x28) {
          Serial.print("Device is not a DS18B20 family device.\n");
          return;
      }
    }

     

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to Robert Peter Oakes

    Thanks Peter that worked great.

    Almost all of the flicker is gone and it updates great thanks. However there still is a noticeable flicker, I'd get a headache if I stared at it too long. Is there any way to get rid of it?

    EDIT: I did some testing if I set the resolution to 12 most of the flicker goes away, however, there is still are random flashes on random digits about twice a second. What is causing this?

     

    Thanks,

    Austin

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago

    It is the rate of refresh versus your eyes perception. If the rate slows down too much, you see flickers.

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to clem57

    It's not as much of a flicker as it is a flash. About twice a second a random digit flashes brighter than the others for a second. There doesn't seem to be an order it's all random.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago in reply to Former Member

    Code in os's runs random which may be your case.

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube