Trouble with Analog In & Mapping - Solved

MEGA 2560 R3
I have a temp probe (non-arduino) that shows the temp 78F on an old weather station LCD display, and it works fine. I want to also show the temp on a large LCD of my own.

I exposed the data and ground wires from the probe bundle. I connected data to A6 and ground to arduino grd. I also hooked my digital VM to the same two wires. It shows a very consistent 0.832v at 78F.

95F 0.62v
90F 0.68v
78F 0.83v
60F 1.09v
34F 1.56v

34 to 95 is all the range I need.

Being that A6 uses a 5v ref voltage, I would expect to see something on the order of 170 when I Serial.println(analogRead(A6); .83/5v * 1024. But instead I get values around 300.

My large LCD works fine and shows a temperature, just not the right one. My room temp is 78F, shown on two independent thermometers, so i know that is right. The large LCD shows 87F.

As nearly as I can tell, there are two issues. One, why am I not reading the correct analogin value, and two; how can i map something like this. The voltages drop as temp increases.

at an earlier time i had used the INTERNAL2v56 command or statement in an effort to help with mapping, but it did not. Although I removed it and now include analogReference(DEFAULT); in the setup, I am wondering if i used the previous command wrong and set a fuse or something.

For now I am setting the mapping issue aside and concentrating on why I am not getting the correct voltages from analogRead.

Any thoughts or suggestions ? attached is an image of a portion of the Serial print.

#include <Adafruit_GFX.h>    // Core graphics library
#include <MCUFRIEND_kbv.h>   // Hardware-specific library

MCUFRIEND_kbv tft;

#include <FreeDefaultFonts.h>

#define BLACK   0x0000  // U16 definition
#define RED     0xF800
#define GREEN   0x07E0
#define DARKGREEN 0x0408
#define WHITE   0xFFFF
#define GREY    0x8410
#define YELLOW  0xFFE0
#define BLUE    0x001F
#define ORANGE  0xFBE0
#define LIGHTCYAN 0x87FF
#define PINK 0xF81F

int temp;

void setup(void)
{  
  analogReference(DEFAULT);
    pinMode(A6,INPUT);
    Serial.begin(9600);
    tft.begin(0x9486);
    tft.setRotation(1);
    tft.fillScreen(BLACK);
 }

void loop(void)
{
    int i =0;
    float subSensor = 0;
    
    for(int i = 1; i<10; i=i+1){
     delay(1000);
     float sensorValue = analogRead(A6);
     Serial.println(i);
     Serial.println(sensorValue);
     Serial.println("");
     subSensor = subSensor + sensorValue;
      }
        
     float Atemp;
     Serial.println(subSensor);
     Atemp=subSensor/9;
     Serial.println(Atemp);
                                                
     temp = map(Atemp,150, 578, 90, 62);  // 
     Serial.print("Temp = ");
    Serial.println(temp);
    Serial.println("");
    i=1;
}

The values are quite spread (by about +/- 10%), so I guess the sensor output isn't compatible to be read by an ADC. Maybe it just need an amplification and then it reads without any problem on the Arduino but without knowing details about your sensor it's hard to tell.

how can i map something like this. The voltages drop as temp increases.

The map() function also works if argument 2 is greater than argument 3 or if argument 4 is greater than argument 5.

when you say, the values, i assume you are talking about the measure voltages. those were just key points i picked. the voltage is linear between all temperatures. see the attached chart image.

i hooked the data line to my oscope and took a couple of pictures. the display is very erratic. both the MEGA and the NANO show erratic analogRead values. in looking at the oscope display (see attached), i am not surprised. what bugs me is how the Weather Wizard III doesn’t seem to have the same problem. it shows a stable resulting temperature.

maybe there is some filter being used. any suggestions ?

2020-01-16 12 47.png

the other two images got deleted due to space. i’ll try again.

I always have a heck of a time when i try attaching images.

and again.

Is that the Weather Wizard III with the temperature sensor on the 25 foot (7.6m) cable? The cable may be acting as an antenna...

yes it is. but i have the cable coiled up by my test bench.

when you say, the values, i assume you are talking about the measure voltages. those were just key points i picked. the voltage is linear between all temperatures. see the attached chart image.

No, I meant the ten values in the serial output of the screenshot.

maybe there is some filter being used. any suggestions ?

Use a big capacitor over the sensor signal to flatten it out. It’s a rather strange signal graph for a temperature sensor…

yes, it is a strange signal, and i guess it is my fault. i finally figured out that if i have the usb connected to the MEGA, i get that junk signal on the data line when using A6. i changed to A13 because i can get a better connection under the large LCD shield. now i got a reliable DC signal and was able to fine tune the mapping.

but when i powered the board with 12v from the jack, the LCD display and temperature worked, but the mapping was off. reading that the MEGA can be powered using up to 12v, i thought maybe i'd try a lower input voltage and see if it had any effect. i used 9v, and bang, perfect mapping and accurate temps from 28F up to 98F.

i was so pleased and relieved that i saved the sketch in 3 different names, didn't touch any of the wiring, and drank myself to sleep staring at that beautiful Large, Accurate screen.

Sweet. Thanks for coming back and posting your solution.

Actually, now you mention it, I have seen that behaviour myself when connected to USB; should've remembered.