Mental block: trying to work out range for calibration formulae

It's always nice to have a clear, digital readout of temperature, but for my latest project I am going all retro, and am building an analogue temperature indicator using a 0-5V voltmeter.

I'm having a blond moment and need some clarification on how I figure out the calibration formulae for the sensor I am using on my Arduino. Hoping someone can help!

So, I am going to use a DS18B20 temperature sensor, with my Arduino and a 0-5V analogue voltmeter. I am trying to work out the calibration factor. Here's my assumptions and calculations that I need some assistance on please...

The temperature sensor has a range of -55 to +125 °C and can be programmed to output a 9 bit through 12 bit resolution. I will use the 12 bit resolution, so -55°C = 0, 125°C = 4096.

-55°C - 0 and 125°C = 4095

The range of temperatures I want to display for my location on the analogue voltmeter (0-5V) is -5°C to +35°C and so I want to calibrate a Digital PWM out on my Arduino to output 0V when the temperature is -5°C, and 5V when the temperature is +35°C.

The range of the sensor is 180 degrees across 12 bits, and I want to use a range of 40 degrees which is

40/180*4096 = 910 bits of the 4096 bits in total, which is almost enough to use with the 10-bit range of the Arduino analogue input.

So, on the 0-4096 bits of temperature sensor output, I only want to use from bit 1365 to bit 1980 as my 0-1024 analogue input, then mapped to the 0-255 on the PWM 0-5V output.

Phew... :o

Is this possible? How do I map it please?

It's a Friday evening after all :)

So, on the 0-4096 bits

I hope you mean 12 bits. (0 to 4095)

AWOL: I hope you mean 12 bits. (0 to 4095)

Do I really need to correct that??

*sigh

theMusicMan:
Do I really need to correct that??

*sigh

Yes, please, go ahead.

(Interesting point: 24096 is an uncomfortably larger number than the number of fundamental particles in the observable universe)

AWOL: Yes, please, go ahead.

We are in a pedantic mood this evening, eh! :)

Given earlier in my post I mentioned "-55°C - 0 and 125°C = 4095" - which suggests I am fully aware of the range of 12-bits. But thanks for pointing it out this minor indescretion which would yield in a 0.0244% error.

And surely you have more important and significantly less inane things to be getting on with?

You say “pedantic”, I say “realistic”.

Let’s call the whole thing off.

PWM does not make analog voltage output. It makes pulses of VCC output.

You could probably wire an LM35 into a circuit that would give the VM 0-5V analog.

GoForSmoke: PWM does not make analog voltage output. It makes pulses of VCC output.

You could probably wire an LM35 into a circuit that would give the VM 0-5V analog.

Sure, I am just playing around really in my efforts to learn coding and electronics etc, and the 0-5V analogue type meter I have does display PWM 0-5V output.

What I am wanting to do is simply read the relevant range of the the 12-bits of the sensor using the 10-bit anaolgue input of the Arduino, and then output that on one of the digital pins as a PWM 0-5V output.

This gives me the greatest accuracy in temperature readings on the scale of temperatures I want to read as instead of having the dial move in increments of 1°C, it will use the 0-255 across the entire -5 to +35 temp scale.

40/180*4096 = 910 bits of the 4096 bits in total, which is almost enough to use with the 10-bit range of the Arduino analogue input.

Since the DS18B20 is digital, the 10-bit Arduino ADC has nothing to do with this...

So, on the 0-4096 bits of temperature sensor output, I only want to use from bit 1365 to bit 1980 as my 0-1024 analogue input, then mapped to the 0-255 on the PWM 0-5V output.

Try this -

map(X, 1365, 1980, 0, 255); //Not tested or verified!!!

...But first, read the raw temperature sensor and "look at" it with the serial monitor to make sure room temperature (or whatever) looks reasonable.

DVDdoug:
Since the DS18B20 is digital, the 10-bit Arduino ADC has nothing to do with this…

I thought it would because I am trying to map the 910 bits of the sensor from bits 1365 to 1980 as bits 0-1024 of the analogue input?

DVDdoug:
Try this -

map(X, 1365, 1980, 0, 255); //Not tested or verified!!!

…But first, read the raw temperature sensor and “look at” it with the serial monitor to make sure room temperature (or whatever) looks reasonable.

I shall… thanks for the pointers.

10 bits can be 0 to 1023. Not 1023 bits, MAX VALUE is 1023.

12 bits can be 0 to 4095. Divide that by 4 and get 10 bit values.

And regular PWM outputs 0 to 100% as 0 to 255 which is 8 bits.

You need to map a range from the 12 bit DIGITAL input to the 8 bit PWM, not the 10 bit ADC.

GoForSmoke: 10 bits can be 0 to 1023. Not 1023 bits, MAX VALUE is 1023.

12 bits can be 0 to 4095. Divide that by 4 and get 10 bit values.

And regular PWM outputs 0 to 100% as 0 to 255 which is 8 bits.

You need to map a range from the 12 bit DIGITAL input to the 8 bit PWM, not the 10 bit ADC.

Yep, I get that MAX VALUE thing, just didn't type it correctly.

However, what I don't get is that the Arduino input is not capable of reading the resolution of a 12-bit sensor, as it is only 10-bit, and in addition I only want to map a range of the temp sensors 0-4095 values to the full Arduino PWM 0-255, which will yield greater accuracy.

I am still having a blond moment! :)

It’s only the ADC which is ten bit, but with a twelve bit digital sensor, you’re not using the ADC.

AWOL: It's only the ADC which is ten bit, but with a twelve bit digital sensor, you're not using the ADC.

Ok, I understand that bit.

But... I still have to feed the output of the 12-bit sensor into the 10-bits of the Arduino analogue in, and what I want to try to do for greater accuracy in temperature measurement is to have the range of values of 1365 to 1980 from the temperature sensor mapped through to the full 0-255 PWM output - not the full 4096 values .

No, sorry, I really don't see what the problem is here.

Lets see if this can chase the Blonde away:

Your 12 bit temperature data arrives as a serial bit stream via one digital input pin, it never passes through an analog input pin. Therefore, FORGET about the 10bit analog reasolution of the ADC, it's not a factor.

The map function is all that you need to achieve your stated goal. 12 bit arbitrary range in, 8 bits out.

Digital bit stream (0-4095) read in, map, (0-255) pwm analog out. Done.

as a PS, it does not matter if the [u]range[/u] of your desired sensor data is only using 8/9/10/11 whatever bits of the source data. Obviously you're compressing the range of the source data to display on a limited scale. You could take it to the extreme and only use 5 degrees of display range. Just calculate the corresponding input values for the MAP function and presto, a 5 degree span thermometer.

BaDing! hear that? Hopefully that's the 'I've got it' bell!

I think I get now what I hadn't realised previously. I was thinking the device was needed to be used on an Arduino analogue input pin, now I see it is a serial 9 to 12-bit data device.

*hides in corner!

avr_fred: Digital bit stream (0-4095) read in, map, (0-255) pwm analog out. Done.

Ahh, but if I did that, the analogue gauge would read 0V (-55°C) at 0 and 5V (125*C) at 4096, so my 0-5V range would be entirely not what I want to display.

theMusicMan: Ahh, but if I did that, the analogue gauge would read 0V (-55°C) at 0 and 5V (125*C) at 4096, so my 0-5V range would be entirely not what I want to display.

That's why I added the ps. Just to insure all blondness was erraticated.