MCP9808 coding help

Hello everyone! im fairly new to coding and was wondering how i can create (or how to start) a program that compiles 5 average temperature readings taken every second and display that final average temperature on the serial console using the MCP9808 sensor.

I already have the board and component wired up and a library already in use. Was just wondering how i would program the sensor to take the average of 5 temperature readings and display the final temperature in C.

thanks for the help!

Sum up 5 consecutive readings and divide the result by 5.

1 Like

sorry im absolutely new to this, how would i got about doing this? thanks so much for the help

int sum = analogRead(analoPin);
sum += analogRead(analoPin);
sum += analogRead(analoPin);
sum += analogRead(analoPin);
sum += analogRead(analoPin);
analogAverageValue = sum / 5;

Do you know how to take a single reading, and store it in a variable?

If not, fix that first.

First please fix your post above. You should always use code tags when posting code. The forum guide explains how. Without the tags, the forum software can mess up the code you post, making it difficult to read or making it look like you made mistakes that are not in your actual code.

Also please format your code before you post it again by clicking Tools->Auto Format.

Would this do?

float c = tempsensor.readTempC();
        c += tempsensor.readTempC();
        c += tempsensor.readTempC();
        c += tempsensor.readTempC();
        c += tempsensor.readTempC();
        c = c / 5.0;
    Serial.print("Temp: "); 

Thanks for fixing the code tags and formatting!

Do you mean 5 readings in 1 second or 5 readings in 5 seconds?

Either way, you can use the code @Railroader suggested but with a delay() between each reading. The length of the delay depends on the question above: either delay(200) or delay (1000).

thank you for the advice! and yes so essentially the goal is for the sensor to take 5 readings in 1 second and then give out the average of those 5 within that 1 second

I'm not sure that its taking the sum and averaging the 5 readings. It's still only giving me readings one by one even when I add delays() to meet the parameters of the program (5 readings in 1 second turned to 1 final average after that second)

We need to see "it"

Why?

Here's a link to the datasheet: MCP9808 - +/-0.5°C Maximum Accuracy Digital Temperature Sensor (octopart.com)

Look at page 3! There it tells how many conversions the unit makes per second. Using delay to make sure a new conversion is done, it ought to work. Try and make a Serial.print after each reading.

sum = sensor.read();
delay(???);
Serial.println(sum);
tmp a = sensor.read();
delay(???);
Serial.println(tmp);
Sum += tmp; 
tmp a = sensor.read();
delay(???);
Serial.println(tmp);
Sum += tmp; 
tmp a = sensor.read();
delay(???);
Serial.println(tmp);
Sum += tmp; 
tmp a = sensor.read();
Serial.println(tmp);
Sum += tmp; 
Value = Sum / 5;

No, the "it" that's producing that :roll_eyes:

Why do you need to wake up the sensor again and again?

If you answer because you shut it down again and again (waiting on seeing your code!) why do you shut it down, ever? Just wake it up in your setup() function once.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.