Skip to content

4 test

Assignments | Week 13 | Embedded Networking and Communications

Group assignment

  • Send a message between two projects
  • Document your work to the group work page and reflect on your individual page what you learned

Individual assignment

  • design, build, and connect wired or wireless node(s) with network or bus addressesld, and connect wired or wireless node(s) with network or bus addresses

Overview

Test and programming

The board is now ready for coding. Let’s GOOOOOOOOO!

LED ON

void setup() {
  \\ The LED will be on permanently
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
}

void loop() {

}

The program worked

First try: Get Thermistor’s data

I tested the code from this web article about how to use a thermistor: https://learn.adafruit.com/thermistor/using-a-thermistor

Don’t forget to attribute if you would like to use it too.

// SPDX-FileCopyrightText: 2011 Limor Fried/ladyada for Adafruit Industries
//
// SPDX-License-Identifier: MIT

// thermistor-1.ino Simple test program for a thermistor for Adafruit Learning System
// https://learn.adafruit.com/thermistor/using-a-thermistor by Limor Fried, Adafruit Industries
// MIT License - please keep attribution and consider buying parts from Adafruit

#include <math.h>

# define LED 4 

// the value of the 'other' resistor
#define SERIESRESISTOR 10000     
// What pin to connect the sensor to
#define THERMISTORPIN A0 

void setup() {
  //PORTA.DIR |= PIN3_bm; 
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);

  Serial.begin(9600);   /* Define baud rate for serial communication */

}

void loop() {
  float reading;

  reading = analogRead(THERMISTORPIN);

  Serial.print("Analog reading "); 
  Serial.println(reading);

  // convert the value to resistance
  reading = (1023 / reading)  - 1;     // (1023/ADC - 1) 
  reading = SERIESRESISTOR / reading;  // 10K / (1023/ADC - 1)
  Serial.print("Thermistor resistance "); 
  Serial.println(reading);

  delay(1000);
}

The serial output shows the value from reading the analog

So far, I just copied the code without asking why. Because I would like to have a quick feedback from the thermistor to make sure that it works.

Now, I’m going to write down all the things that needs more explaination.

Convert the value readed to resistance and temperature

As mentioned in the Research part, there are 2 types of thermistor: the Negative Temperature Coefficient (NTC) and Positive Temperature Coefficient (PTC). Here we are using an NTC thermistor. It means that when its temperature increases, its resistance decreases. What does it involve?

Change in resistance means a change in voltages. We can use an Arduino analog pin to measure that voltage changes and from that, we can calculate the temperature value.

The AnalogRead doesn’t read the temperature but the voltage from the thermistor;

We need to do some calculations to get the temperature value from that data.

I looked around on the web, and noticed that the Steinhart–Hart equation is used widely:

  • https://www.circuitgeeks.com/arduino-temperature-sensor-thermistor/
  • https://arduino-france.site/thermistance/

voltage divider method

https://solarduino.com/how-to-use-ntc-thermistor-to-measure-temperature/

Final code

// SPDX-FileCopyrightText: 2011 Limor Fried/ladyada for Adafruit Industries
//
// SPDX-License-Identifier: MIT

// thermistor-1.ino Simple test program for a thermistor for Adafruit Learning System
// https://learn.adafruit.com/thermistor/using-a-thermistor by Limor Fried, Adafruit Industries
// MIT License - please keep attribution and consider buying parts from Adafruit

#include <math.h>

# define LED 4 

// the value of the 'other' resistor
# define SERIESRESISTOR 10000     
// What pin to connect the sensor to
# define THERMISTORPIN A0 

# define B 3750

# define Rinf 0.03448//10000*exp(-3750/298.15)

void setup() {
  //PORTA.DIR |= PIN3_bm; 
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);

  Serial.begin(9600);   /* Define baud rate for serial communication */

}

void loop() {
  float reading;
  float R;
  float T; 

  reading = analogRead(THERMISTORPIN);

  Serial.print("Analog reading "); 
  Serial.println(reading);

  // convert the value to resistance
  R = (1023 / reading)  - 1;     // (1023/ADC - 1) 
  R = SERIESRESISTOR / R;  // 10K / (1023/ADC - 1)
  Serial.print("Thermistor resistance "); 
  Serial.println(R);

  T = R/Rinf;
  T = log(T);
  T = B/T;
  T = T-273.15;

  Serial.print("Temperature ");  
  Serial.println(T); 

  delay(1000);
}

T(°C) = T(K) - 273,15

the article: https://www.electronicwings.com/arduino/thermistor-interfacing-with-arduino-uno

The Final code

// which analog pin to connect
#define THERMISTORPIN 2

void setup() {
  pinMode(4, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);

  int thermistor_adc_val = analogRead(THERMISTORPIN);

  double output_voltage = (thermistor_adc_val * 5.0) / 1023.0;

  double thermistor_resistance = (5 * (10.0 / output_voltage)) - 10;

  thermistor_resistance = thermistor_resistance * 1000;

  double therm_res_ln = log(thermistor_resistance);

  double temperature = 1 / (0.001129148 + (0.000234125 * therm_res_ln) + (0.0000000876741 * therm_res_ln * therm_res_ln * therm_res_ln));

  temperature = temperature - 273.15;

  Serial.print("Temperature in degree Celsius = ");
  Serial.print(temperature);
  Serial.print("\t\t");
  Serial.print("Resistance in ohms = ");
  Serial.print(thermistor_resistance);
  Serial.print("\n\n");

  delay(1000);
}

OUTPUT: