3
\$\begingroup\$

I tried to make a sensing system using arduino UNO R3 with LM35 sensor and a display unit which is seven segment with two digits.

I made all the connections as follow(I know it is Missy but I think wiring is right) : enter image description here

and I tried to make this program but is didn't work :

int analogPin = 3;     // potentiometer wiper (middle terminal) connected to analog pin 3
                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read

byte seven_seg_digits[10][7] =                                                           
                                                            { 
                                                           { 1,1,1,1,1,1,0 },  // = 00
                                                           { 0,1,1,0,0,0,0 },  // = 01
                                                           { 1,1,0,1,1,0,1 },  // = 02
                                                           { 1,1,1,1,0,0,1 },  // = 03
                                                           { 0,1,1,0,0,1,1 },  // = 04
                                                           { 1,0,1,1,0,1,1 },  // = 05
                                                           { 1,0,1,1,1,1,1 },  // = 06
                                                           { 1,1,1,0,0,0,0 },  // = 07
                                                           { 1,1,1,1,1,1,1 },  // = 08
                                                           { 1,1,1,0,0,1,1 },   // = 09
                                                            };
    void setup()
{
  Serial.begin(9600);          //  setup serial
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT); 
  pinMode(2, OUTPUT);  
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);  
  pinMode(12, OUTPUT);  
  pinMode(13, OUTPUT);  

}

void loop()
{

  val = analogRead(analogPin);    // read the input pin
  float temp= val*5 ; //calculating the voltage
  temp /= 1024 ;    //calculating the voltage
  temp = (temp) * 100 ;// as 1V/100c
  int pin=0 ; //for first seven segment pins 0,1,2,3,4,5,6 is connected with a,b,c,d,e,s,g
  int pin1=7;
  Serial.println("temperature in C =");Serial.println(temp);             // debug value
  int tempL; //low number
  tempL=int(temp*10);
  int tempH;//high number
  tempH=int(temp/10);
  for(int j=0;j<7;j++) 
  {
    digitalWrite(pin,seven_seg_digits[tempL-1][j]);
  pin++;}
  for(int i=0;i<7;i++)
  {
   digitalWrite(pin1,seven_seg_digits[tempL-1][i]);
 pin1++;}
  delay(2000);
}

I have tow problem : 1- pin 0 and pin 1 always high . 2- when I calculate the temperature from converting the returned number to volt then to temp I had wrong answer as I see from monitor and the number of the display is wrong too , the number that appear don't realat even to

when I see the output of LM35 using voltage-meter I see 0.3[v] which is sensible because it is summer and temperature about 30 here in middle east .

this video I recorder live(excuse me for my bad English) to explain more : http://youtu.be/iQobAe4ie_8

and I hope if any one can explain about "offset" that this link talk about .

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Please add what you see in the monitor. \$\endgroup\$ – jippie Jul 1 '12 at 20:17
  • \$\begingroup\$ @jippie temperature in C = 109.37 temperature in C = 101.07 temperature in C = 118.65 temperature in C = 116.21 temperature in C = 115.23 temperature in C = 104.49 \$\endgroup\$ – yahya tawil Jul 1 '12 at 20:30
  • \$\begingroup\$ Are you sure the program in your question above is the same as the version you're running? What values do you expect? Please add the answer to the initial question. \$\endgroup\$ – jippie Jul 1 '12 at 20:35
  • \$\begingroup\$ yes of course it the same \$\endgroup\$ – yahya tawil Jul 1 '12 at 20:45
  • \$\begingroup\$ @jippie I am sorry I missed copy the code ... the a above is after changing from int to float \$\endgroup\$ – yahya tawil Jul 1 '12 at 20:51
5
\$\begingroup\$

Guessing about what the problem is you're facing as you don't give enough details.

int val = 0;
val = analogRead(analogPin);    // read the input pin
int temp= val*5 ; //calculating the voltage
temp /= 1024 ;    //calculating the voltage
temp = (temp) * 100 ;// as 1V/100c

You are using integer math, for Arduino this means both val and temp are whole numbers ranging from -32768 to +32767.

You read a value from ADC, this value ranges from 0-1023 and you did not set the reference voltage. This means that reading 0 equals GND / 0V and reading 1023 equals +Vdd = 5V (approx. varying with your power supply, possibly being USB).

Say your supply voltage is 5V and the voltage at the input pin is 0.7V. The analogRead(analogPin); will return 1024 × 0.7 / 5 = 143.

Next you multiply that number: 143 × 5 = 715, so far so good.

Then you divide this number by 1024, this is where your program goes wrong: 715 / 1024 = 0.689 but we are doing integer math, and integers hold whole numbers only and all numbers after the decimal point will be chopped off! So temp is 0 at this point. It doesn't matter how much you multiply the result from this point, it will always be 0.

Change int temp to float temp, that should solve your issue. It instructs the compiler not to use integers, but to use a floating number instead.

There is an error here too:

int tempL; //low number
tempL=int(temp*10);
int tempH;//high number
tempH=int(temp/10);

Change it to:

int tempH;//high number
tempH=int(temp/10);
int tempL; //low number
tempL=int(temp-tempH*10);
\$\endgroup\$

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.