Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. AnalogRead problem

AnalogRead problem

Scheduled Pinned Locked Moved Troubleshooting
12 Posts 5 Posters 172 Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MasMatM Offline
    MasMatM Offline
    MasMat
    wrote on last edited by
    #3

    Same result float => int change in the sketch.

    Tried to reupload bootloader but got a problem, same with another atmega328. Will investigate that next.

    1 Reply Last reply
    0
    • skywatchS Offline
      skywatchS Offline
      skywatch
      wrote on last edited by
      #4

      @MasMat said in AnalogRead problem:

      analogReference(INTERNAL);

      Won't that set the analog reference to the 1.1V internal source? If so then the analog read range of 0-1023 will be mapped to 0-1.1V. Anything over 1.1 will be seen as 1.1V - But maybe I'm wrong....???

      1 Reply Last reply
      0
      • MasMatM Offline
        MasMatM Offline
        MasMat
        wrote on last edited by MasMat
        #5

        I investigated the bootloader problem, I use 1.8.1 definitions that made those problems disappear. But still, the A2 (THMR_PIN) reads 1023 but the battery sense on A4 works.

        EDIT: now I get what you mean: it also changes the reference for the whole ADC...from 3.3 to 1.1V! Any ideas for changing the resistors? I would want to use the same thermistor as I only have two wires going to the sauna. OR is there a way to change the reference for A2 read and go back after it's been read?

        1 Reply Last reply
        0
        • electrikE Offline
          electrikE Offline
          electrik
          wrote on last edited by
          #6

          Yes, you can change between references.

          analogReference(DEFAULT);
          

          Be aware that after you changed the reference the first 52 reads have to be discarded.

          MasMatM 1 Reply Last reply
          0
          • electrikE electrik

            Yes, you can change between references.

            analogReference(DEFAULT);
            

            Be aware that after you changed the reference the first 52 reads have to be discarded.

            MasMatM Offline
            MasMatM Offline
            MasMat
            wrote on last edited by MasMat
            #7

            @electrik ..and then just adjust the volts/bit for 3.3 rather than 1.1v, correct? (i.e x3). That fixed it, thank you.
            BUT presented a weird problem. The analogRead gives a proper value but the math then is not working and I cant figure it out. I did the calculations myself and get a proper result:

            int _nominal_resistor2 = 10000;
              int _nominal_temperature2 = 25;
              int _b_coefficient2 = 3950;
              int _series_resistor2 = 10000;
              int adcTmm = analogRead(THMR_PIN);
              Serial.println(adcTmm);
              float readingSau = (1023 / adcTmm)  - 1;
              readingSau = _series_resistor2 / readingSau;
              float temperatureS = readingSau / _nominal_resistor2;     // (R/Ro)
              temperatureS = log(temperatureS);                  // ln(R/Ro)
              temperatureS /= _b_coefficient2;                   // 1/B * ln(R/Ro)
              temperatureS += 1.0 / (_nominal_temperature2 + 273.15); // + (1/To)
              temperatureS = 1.0 / temperatureS;                 // Invert
              temperatureS -= 273.15;                         // convert to C
              send(msgTempSau.set((int16_t)temperatureS, 2));
                Serial.print("Sauna T: ");
                Serial.print(temperatureS);
                Serial.println(" C");
            

            But still the serial monitor shows:

            7409 MCO:SLP:WUP=1
            7413 TSF:TRI:TSB
            Sauna door: 1
            7430 TSF:MSG:SEND,21-21-10-0,s=3,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
            7966 TSF:MSG:SEND,21-21-10-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:29.00
            Shower T: 29.00
            7989 TSF:MSG:SEND,21-21-10-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:87.0
            Shower H: 87.00
            536
            8005 TSF:MSG:SEND,21-21-10-0,s=16,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:-273.00
            Sauna T: -273.15 C
            Sauna off, sleeping 30min
            Battery Voltage: 4.22 V   Battery percent: 97.70 %
            8028 TSF:MSG:SEND,21-21-10-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:97
            8097 MCO:SLP:MS=1800000,SMS=0,I1=1,M1=1,I2=255,M2=255
            8108 TSF:TDI:TSL
            

            I changed variable names to account there wouldnt be overlap with DHT library & dht part of sketch.

            YveauxY 1 Reply Last reply
            0
            • electrikE Offline
              electrikE Offline
              electrik
              wrote on last edited by electrik
              #8

              These kind of issues are usually caused by an overflow somewhere, or memory being full

              Edit:
              Also check if you're using the right log function... There are two log functions, the log base 10 and the log base 2

              MasMatM 1 Reply Last reply
              0
              • electrikE electrik

                These kind of issues are usually caused by an overflow somewhere, or memory being full

                Edit:
                Also check if you're using the right log function... There are two log functions, the log base 10 and the log base 2

                MasMatM Offline
                MasMatM Offline
                MasMat
                wrote on last edited by MasMat
                #9

                @electrik Thanks I'll check those.
                I added a lot of serial.Print's to debug where it goes south and it appears to ruin already at the first division after the analogRead (I started receiving 530's analogRead at room temp). So probably something else completely that's rotten. I'll try another machine with another Arduino IDE. It should work now that the original question is answered.
                So I'll consider this solved. Thanks again!

                boumB 1 Reply Last reply
                0
                • MasMatM MasMat

                  @electrik Thanks I'll check those.
                  I added a lot of serial.Print's to debug where it goes south and it appears to ruin already at the first division after the analogRead (I started receiving 530's analogRead at room temp). So probably something else completely that's rotten. I'll try another machine with another Arduino IDE. It should work now that the original question is answered.
                  So I'll consider this solved. Thanks again!

                  boumB Offline
                  boumB Offline
                  boum
                  wrote on last edited by
                  #10

                  @MasMat
                  The integer division might be the cause. Please try to force conversion to floating point:

                    float readingSau = (1023.f / adcTmm)  - 1.f;
                  
                  1 Reply Last reply
                  1
                  • MasMatM MasMat

                    @electrik ..and then just adjust the volts/bit for 3.3 rather than 1.1v, correct? (i.e x3). That fixed it, thank you.
                    BUT presented a weird problem. The analogRead gives a proper value but the math then is not working and I cant figure it out. I did the calculations myself and get a proper result:

                    int _nominal_resistor2 = 10000;
                      int _nominal_temperature2 = 25;
                      int _b_coefficient2 = 3950;
                      int _series_resistor2 = 10000;
                      int adcTmm = analogRead(THMR_PIN);
                      Serial.println(adcTmm);
                      float readingSau = (1023 / adcTmm)  - 1;
                      readingSau = _series_resistor2 / readingSau;
                      float temperatureS = readingSau / _nominal_resistor2;     // (R/Ro)
                      temperatureS = log(temperatureS);                  // ln(R/Ro)
                      temperatureS /= _b_coefficient2;                   // 1/B * ln(R/Ro)
                      temperatureS += 1.0 / (_nominal_temperature2 + 273.15); // + (1/To)
                      temperatureS = 1.0 / temperatureS;                 // Invert
                      temperatureS -= 273.15;                         // convert to C
                      send(msgTempSau.set((int16_t)temperatureS, 2));
                        Serial.print("Sauna T: ");
                        Serial.print(temperatureS);
                        Serial.println(" C");
                    

                    But still the serial monitor shows:

                    7409 MCO:SLP:WUP=1
                    7413 TSF:TRI:TSB
                    Sauna door: 1
                    7430 TSF:MSG:SEND,21-21-10-0,s=3,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
                    7966 TSF:MSG:SEND,21-21-10-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:29.00
                    Shower T: 29.00
                    7989 TSF:MSG:SEND,21-21-10-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:87.0
                    Shower H: 87.00
                    536
                    8005 TSF:MSG:SEND,21-21-10-0,s=16,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:-273.00
                    Sauna T: -273.15 C
                    Sauna off, sleeping 30min
                    Battery Voltage: 4.22 V   Battery percent: 97.70 %
                    8028 TSF:MSG:SEND,21-21-10-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:97
                    8097 MCO:SLP:MS=1800000,SMS=0,I1=1,M1=1,I2=255,M2=255
                    8108 TSF:TDI:TSL
                    

                    I changed variable names to account there wouldnt be overlap with DHT library & dht part of sketch.

                    YveauxY Offline
                    YveauxY Offline
                    Yveaux
                    Mod
                    wrote on last edited by
                    #11

                    @MasMat Try changing

                    float readingSau = (1023 / adcTmm)  - 1;
                    

                    To

                    float readingSau = (1023.0 / float(adcTmm) )  - 1;
                    

                    You are mixing integer and floating point math.

                    http://yveaux.blogspot.nl

                    MasMatM 1 Reply Last reply
                    2
                    • YveauxY Yveaux

                      @MasMat Try changing

                      float readingSau = (1023 / adcTmm)  - 1;
                      

                      To

                      float readingSau = (1023.0 / float(adcTmm) )  - 1;
                      

                      You are mixing integer and floating point math.

                      MasMatM Offline
                      MasMatM Offline
                      MasMat
                      wrote on last edited by
                      #12

                      @Yveaux and @boum You two beat me by 2hrs.
                      @user2684 whose code this is (cudos to him, have used this bit on a number of sensors!) had probably figured that out and I screwed myself when I changed the original analogRead to int.

                      Now the whole thing works 100%

                      1 Reply Last reply
                      2
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      27

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.1k

                      Posts


                      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular