2

I am trying to code my 12 pin seven segment display to display the temperature from my grove temperature sensor. I am new to this and I have been struggling, so if anyone could help that would be much appreciated.

My temperature sensor is displaying its information in the serial monitor, however I want the temperature displayed with the 7 segment display - that's where I need help.

//Temperature Variables
//thermistor = value of the thermistor
int a;
float temperature;
int thermistor=3975;
float resistance;
boolean DigitOn = LOW;
boolean DigitOff = HIGH;
boolean SegOn=HIGH;
boolean SegOff=LOW;

int DigitPins[] = {2, 3, 4, 5};
int SegmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};

//N is for numbers and NxP is a number with a decimal point behind
int BLANK[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
int N0[]    = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW};
int N0P[]   = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH};
int N1[]    = {LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW};
int N1P[]   = {LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, HIGH};
int N2[]    = {HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH, LOW};
int N2P[]   = {HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH};
int N3[]    = {HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH, LOW};
int N3P[]   = {HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH, HIGH};
int N4[]    = {LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH, LOW};
int N4P[]   = {LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH, HIGH};
int N5[]    = {HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH, LOW};
int N5P[]   = {HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH, HIGH};
int N6[]    = {HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
int N6P[]   = {HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int N7[]    = {HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW};
int N7P[]   = {HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW, HIGH};
int N8[]    = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
int N8P[]   = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int N9[]    = {HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, LOW};
int N9P[]   = {HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH};
int MIN[]   = {LOW, LOW, LOW, LOW, LOW, LOW, HIGH, LOW};

//Array of pointers for the 4 digits
int* lights[4];

//char array coming from the serial interface
//4 numbers or chars, 4 optional decimal points, 1 end-of-line char
char incoming[9] = {};

void setup() {
  Serial.begin(19200);
  for (byte digit=0;digit<4;digit++) {
    pinMode(DigitPins[digit], OUTPUT);
  }

  for (byte seg=0;seg<8;seg++) {
    pinMode(SegmentPins[seg], OUTPUT);
  }

  //initialize display with 1.234
  lights[0] = N7;
  lights[1] = N9P;
  lights[2] = N3;
  lights[3] = N4;
}

void loop() {
  //Temperature Sensor
    a=analogRead(5);
    resistance=(float)(1023-a)*10000/a; //get the resistance of the sensor;
    temperature=1/(log(resistance/10000)/thermistor+1/298.15)-273.15;
//convert to temperature via datasheet&nbsp;;
    //delay(1000);
    Serial.print("Current temperature is ");
    Serial.println(temperature);
    //delay(500);
  //read the numbers and / or chars from the serial interface
  if (Serial.available() > 0) {
    int i = 0;
    //clear the array of char
    memset(incoming, 0, sizeof(incoming));
    while (Serial.available() > 0 && i < sizeof(incoming) - 1) {
      incoming[i] = Serial.read();
      i++;
      delay(3);
    }
    Serial.println(incoming);

    //show the input values
    for (int y = 0; y < 4; y++) {
      Serial.print(y);
      Serial.print(": ");
      for (int z = 0; z < 8; z++) {
        Serial.print(lights[y][z]);
      }
      Serial.println("");
    }
  }

      //end if, i.e. reading from serial interface
      //This part of the code is from the library SevSeg by Dean Reading

  for (byte seg=0;seg<8;seg++) {
        //Turn the relevant segment on
    digitalWrite(SegmentPins[seg],SegOn);
    //For each digit, turn relevant digits on
    for (byte digit=0;digit<4;digit++){
      if (lights[digit][seg]==1) {
        digitalWrite(DigitPins[digit],DigitOn);
      }
      //delay(200); //Uncomment this to see it in slow motion
    }
    //Turn all digits off
    for (byte digit=0;digit<4;digit++){
      digitalWrite(DigitPins[digit],DigitOff);
    }
    //Turn the relevant segment off
    digitalWrite(SegmentPins[seg],SegOff);
  } 
    //end of for
}

migrated from electronics.stackexchange.com Mar 11 '16 at 22:38

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • Add four spaces to the beginning of each line of code, so that the site interprets it correctly. – jms Mar 11 '16 at 20:13
  • You haven't actually asked a question other than how to show images. What's the problem? What are you expecting and what do you get. Please add this information in your question rather than in the comments. – Transistor Mar 11 '16 at 21:01
  • @jms Do not try formatting the text manually. Use the right buttons for that. {} – Avamander Apr 12 '16 at 10:03
  • 1
    I want the temperature displayed with the 7 segment display. - OK. What is happening exactly? – Nick Gammon Apr 16 '16 at 9:45
  • I see that you are using some code from the SevSeg library, but you do not appear to include the header SevSeg.h in your sketch. How are you using the library? – Greenonline Apr 17 '16 at 14:08
0

Your temp sensor is obviously working if it displays the temp on the serial monitor. I would now write a new sketch, forgetting the temp sensor for the moment, and get numerical data on to the seven segment display correctly.

How many digits does the seven segment display have? What interface are you using, if any? How is the display connected to the Arduino? You need to provide the information in your question, to allow us to give you an accurate answer.

You may want to take a look at my answer to Programming 4-digit seven segment display using interrupts only on SE Robotics. I had the same issues, about a year ago, when I was trying to display data on a cheap seven segment display that I purchased in BKK, and I ended up writing an Arduino library in order to get it to work, see Dual Seven Segment Display Madness. However, I don't recommend that you write your own library, and instead use a pre-built one. Take a look at the SevenSeg library. That is what I used eventually.

I see that you are using some code from the SevSeg library, but you do not appear to include the header SevSeg.h in your sketch. If you are happy using the SevSeg library, then first write a sketch using the examples and the tutorials, so that you can get data displayed. Then integrate that software with your temperature sensor software.

You really do need to divide your project into two parts, the sensor and the display. Once you are happy with displaying data on the Seven Segment display, then, and only then can you think about getting the temperature sensor data displayed on it.

0

You loop over the segments. For each segment you set the output high, then pull the digit output low for each digit that should have that segment lit.

The big issue I see is that you turn the digits off immediately after turning them on. The digits are on for only a few processor cycles. If you delay for a few ms before turning all the digits off, you'll probably see them better.

The usual way I've seen this code written, and written it myself, is to loop over digits in the outer loop and set all the segments for one digit at once. You're setting all the digits for one segment. The way you do it, if the display is showing all 8s with decimal points, each segment will be on 1/8 of the time. If you loop over digits, each segment will be on 1/4 the time, and the display will be brighter. Looping over digits lets you illuminate up to 8 segments at once instead of 4.

Finally, doing this with digitalWrite is very inefficient. If you connect the segments to digital ports 0-7 you can set them all simultaneously by writing a byte to PORTD.

0

The simple way is to use a MAX7219 chip which does most of the work for you.

You haven't said what LED device you have but an example of wiring it is:

MAX7219 wiring

That chip does the multiplexing for you. I have a detailed description here.

If you don't want to use another chip you could use 12 of your Arduino pins. It's a little tedious because you need to be refreshing the display even while you are doing other things. You could use a timer interrupt for that.

The code you posted looks like it might work. In what way does it not work?

I have been struggling ...

That doesn't tell us much. Do all segments light up? None? Incorrect ones? Does it flicker? What is the problem exactly?

Please read How do I ask a good question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.