Week 12 : Input Devices
Summary
This week, I tested the addition of a sensor for my final project. I’m not sure exactly which sensors will be used, but it’s to anticipate the measuring and setup.
The result is shown below for the VMA320 sensor.
I also worked with the PCB produced in week 8 and the mlx90614 temperature sensor. The setup and a video of what can be observed as temperature is shown below!
Assignments
Group Assignment
- Probe an input device’s analog levels and digital signals.
Individual Assignments
- Measure something: add a sensor to a microcontroller board that you have designed and read it.
Probe Input Device
This part was done in group and is accessible on the group page.
Adding Input Device to my Board
Contexte of the Week
This week I want to work on my project. It’s more about being able to control plasma generation. I’ll need to measure certain information, such as the temperature of the plasma reactor, or the amount of CO2 leaving the reactor (if the reactor is an open system).
So I’m going to try and retrieve temperature information using two different temperature sensors: VMA320, and VMA324.
These are two analog temperature sensors using a thermistor, a resistor that varies as a function of temperature.
Adding Temperature Sensor
Adding one of these sensors is very simple. You need to supply the sensor with a DC voltage (3.3V or 5V). Then recover the output voltage with a final Pin. A temperature sensor using a thermistor works like a resistive divider. A schematic of the divider is shown below, in which we supply the divider with DC voltage Vin, and recover the output voltage Va, the voltage across the thermistor.
The electronic diagram taken from the documentation is also shown below.
The setup, quite simple, is done separately from the board because it was a bit complicated to do due to a design problem (I wanted to display the temperature on an OLED screen but I didn’t have enough inputs available).
This setup can be seen below for the two sensors used.
From Analog Signal to Temperature
I’ll have to translate the sensor input signal into temperature in the code. To do this, I need to know how thermistors work. It turns out that by knowing the resistive value of the thermistor, we can know the temperature. You also need to know the manufacturer’s reference point, in this case 25°C.
The Steinhart-Hart relationship describes the relationship between resistance and temperature. It is given below.
This law integrates the parameters A, B and C, which are purely empirical and depend on each thermistor.
A simpler version is possible and can be used over a smaller temperature range, incorporating only parameter B and a reference temperature and resistance value. This is what I’m going to use here.
Programming Temp Sensor
So I program the sensor using an adc object to read the sensor’s analog value, which I transform directly into a voltage.
With this voltage I can find out the resistance of the thermistor using ohm law and the reference provided by the sensor datasheet. Then I use the Steinhart-Hart relationship to obtain the absolute temperature and transform it into °C.
import machine
import ssd1306
import time
import math
# Define the pins used
ANALOG_PIN = 26
# Define ref
vRef = 5
tRef = 25
rRef = 10000
# Define Steinhart-Hart parameters
B = 3950
# Initialize the OLED screen
i2c = machine.I2C(1, scl=machine.Pin(7), sda=machine.Pin(6))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Function to read temperature from analog input
def read_temperature(pin):
# Read analog value
adc = machine.ADC(pin)
value = adc.read_u16()
# Convert analog value to voltage
voltage = value / 65535 * vRef # Voltage in volts
# Use the Steinhart-Hart formula to convert voltage to temperature
resistance = (vRef / (vRef - voltage)) * rRef # Resistance in ohms
temperature_kelvin = tRef + 1 / (1 / (273.15 + tRef) + 1 / B * math.log(resistance / rRef) ) # Temperature in Kelvin
temperature_celsius = temperature_kelvin - 273.15 # Temperature in degrees Celsius
return temperature_celsius
while True:
# Read temperature
temperature = read_temperature(ANALOG_PIN)
# Clear the OLED screen
oled.fill(0)
# Display temperature on the OLED screen
oled.text("Temp: {:.2f} C".format(temperature), 0, 0)
oled.show()
# Wait for a few seconds before measuring again
time.sleep(0.1)
Testing
Testing with the Bread Bord Setup
I finally tested the sensors by touching the thermistor to provide heat. One of these tests can be seen in the video below.
Testing with my PCB
I can now test the sensor on the PCB I designed in week 8. It works very similarly to the bread bord, and the result can be seen in the video below.
References
Here are a few references I’ve used to help me. I also used ChatGPT.
- MicroPython Documentation
- Adafruit Learning System
- MicroPython GitHub
- MicroPython Forum
- Stack Overflow - MicroPython
Going Further
To go further and respect the week’s assignments, I’m trying my hand at using another temperature sensor via the I2C by connecting it to the PCB I made in week 8. Below is a picture of the setup. The sensor is an MLX90614 temperature sensor.
To test the sensor, I simply used the code provided on the mlx90614 library git. I adapt the code by selecting the right pins for the i2c connection. Of course, the mlx90614 library for micropython must be installed on the microcontroller.
import time
import mlx90614
from machine import I2C, Pin
i2c = I2C(scl=Pin(7), sda=Pin(6))
sensor = mlx90614.MLX90614(i2c)
while True:
print(sensor.read_ambient_temp(), sensor.read_object_temp())
time.sleep_ms(500)
The code prints the two measured values on the console. A short video below shows what you get.
This completes my work on input devices!