Pressure sensor
Pressure sensor
Applications
Temperature is one of the easiest sensor projects to start with. Applications include:
- Monitoring the temperature of a smart bee hive, rabbit hutch, chick coop, compost heap, worm farm, aquarium or wetland.
- Experimenting with concepts related to energy efficient building construction.
- Temperature compensation for other sensors (sensor values may change with changes in temperature).
- Making a weather station.
Key design considerations
- Circuit was
How to use a pressure sensor
The
DHT22 Internal temperature sensor
Overview
- The DHT22 t
Important notes
- DHT22 Pin layout
- Pin 1 is VCC (Power Supply)
- Pin 2 is DATA (The data signal)
- Pin 3 is NULL (Do not connect)
Parts list
| Item description | Number | Supplier | Part number | URL | Cost | Notes |
|---|---|---|---|---|---|---|
| Solder-able Breadboard | 1 | Core Electronics | SKU: PRT-12070 | https://core-electronics.com.au/solder-able-breadboard.html | $8.20 | 55mm x 80mm |
| Pycom LoPy4 (LoRaWAN) | 1 | Core Electronics | SKU: CE05399 | https://core-electronics.com.au/pycom-lopy4.html | $68.95 | Microcontroller with LoRa |
| Pycom Expansion Board 3.1 | 1 | Core Electronics | SKU: CE05545 | https://core-electronics.com.au/pycom-expansion-board-3-1.html | $35.95 | Used to program LoPy4 |
| DHT22 Temperature and Relative Humidity Sensor Module | 1 | Core Electronics | SKU: 018-DHT22 | https://core-electronics.com.au/dht22-temperature-and-relative-humidity-sensor-module.html | $13.75 | Temp precision +/-0.5degC |
| Temperature Sensor - Waterproof (DS18B20) | 1 | Core Electronics | SKU: SEN-11050 | https://core-electronics.com.au/temperature-sensor-waterproof-ds18b20.html | $18.80 | 1-Wire interface library |
| Electrolytic Decoupling Capacitors - 100uF/25V | 1 | Core Electronics | SKU: COM-00096 | https://core-electronics.com.au/electrolytic-decoupling-capacitors-100uf-25v.html | $0.29 | Used to block high voltage spikes |
| Pololu 5V Step-Up Voltage Regulator U1V11F5 | 1 | Core Electronics | SKU: POLOLU-2562 | https://core-electronics.com.au/pololu-5v-step-up-voltage-regulator-u1v11f5.html | $11.65 | 5V supply for sensors |
| Resistor 10K Ohm 1/4 Watt PTH - 20 pack (Thick Leads) | 1 | Core Electronics | SKU: PRT-14491 | https://core-electronics.com.au/resistor-10k-ohm-1-4-watt-pth-20-pack-thick-leads.html | $2.05 | Pack of 20 resistors |
| Hook-Up Wire - Assortment (Solid Core, 22 AWG) | 1 | Core Electronics | SKU: PRT-11367 | https://core-electronics.com.au/hook-up-wire-assortment-solid-core-22-awg.html | $40.95 | Solid core for wiring circuit |
| 36-pin 0.1 Female header - pack of 5! | 1 | Core Electronics | SKU: ADA598 | https://core-electronics.com.au/20-pin-0-1-female-header-pack-of-5.html | $6.30 | Used for plugin components |
| 10 Pcs 40 Pin Headers - Straight | 1 | Core Electronics | SKU: FIT0084 | https://core-electronics.com.au/10-pcs-40-pin-headers-straight.html | $4.70 | Used to make sensors plugin |
| Holman 100mm PVC DWV Push On Cap | 1 | Bunnings | DWVF0198 | https://www.bunnings.com.au/holman-100mm-pvc-dwv-push-on-cap_p4770360 | $3.30 | End caps for sensor housing |
| Holman 100mm x 3m PVC DWV Pipe | 1 | Bunnings | DWV1003 | https://www.bunnings.com.au/holman-100mm-x-3m-pvc-dwv-pipe_p4770345 | $33.00 | Cut in store for easier transport |
| Selleys 310g Clear Glass Silicone Sealant | 1 | Bunnings | Model no 9300697100382 | https://www.bunnings.com.au/selleys-310g-clear-glass-silicone-sealant_p1234951 | $15.00 | Used with caulking gun |
| 18650 2600mAh Li-ion Protected Battery | 1 | Jaycar | CAT.NO: SB2299 | https://www.jaycar.com.au/18650-2600mah-li-ion-protected-battery/p/SB2299 | $21.95 | Inbuilt safety protection |
| Single 18650 Battery Holder | 1 | Jaycar | CAT.NO: PH9205 | https://www.jaycar.com.au/single-18650-battery-holder/p/PH9205 | $3.25 | Battery holder |
| Dual-Channel Li-ion / Ni-MH Battery Charger | 1 | Jaycar | CAT.NO: MB3635 | https://www.jaycar.com.au/dual-channel-li-ion-ni-mh-battery-charger/p/MB3635 | $39.95 | Only one required for all projects |
| MOLEX 211140-0100.. 868/915MHZ FLEXIBLE ANTENNA100MM CABLE | 1 | Element-14 | Order code 3225220 | https://au.element14.com/molex/211140-0100/868-915mhz-flexible-antenna100mm/dp/3225220?ost=molex++211140-0100..++868%2F915mhz+flexible+antenna100mm+cable | $2.84 | Attached to LoPy4 |
| Through Hole Resistor, 10 kohm, CMF, 500 mW, ± 0.1%, Axial Leaded, 250 V | 2 | Element-14 | Order code 3596907 | https://au.element14.com/vishay/cmf5510k000beea/res-10k-0-5w-axial-metal-film/dp/3596907?ost=vishay++cmf5510k000beea | $1.56 | High precision resistor for voltage divider |
| P9406 • 6 Pin 5A Screw-On Female Line IP66 Waterproof Socket | 1 | Altronics | Code: P9406 | https://www.altronics.com.au/p/p9406-amphenol-ltw-6-pin-5a-screw-on-female-line-ip66-waterproof-socket/ | $23.50 | Connected to sensor housing |
| P9416 • 6 Pin 5A Screw-On Male Chassis IP67 Waterproof Plug | 1 | Altronics | Code P9416 | https://www.altronics.com.au/p/p9416-amphenol-ltw-6-pin-5a-screw-on-male-chassis-ip67-waterproof-plug/ | $17.25 | Connected to sensor |
Circuit schematic
Photos of completed circuit board
- Aerial view of board.
- Close up of board.
- Close up of board without LoPy4.
- Close up of board from DHT22 temperature sensor end.
- Close up aerial view of board without LoPy4.
Electronic circuit construction
Pycom LoPy4
- LED on LoPy4 nearest top end of board.
- Cut female header pins to 14 pin length (x2)
- Solder from pins C10 to C23 and from pins H10 to H23
- VIN (3.5-4.2V direct from battery rail) from positive rail nearest to J to pin I10
- GND from negative rail nearest to J to pin J11
Voltage divider circuit
- 10k ohm high precision resistors x 2
- I16 to positive rail nearest to J
- J16 to negative rail nearest to J
Power supply
- Use either 3 x 1.2V Panasonic eneloop Ni-MH batteries
- Or use 1 x 3.7V Li-Ion battery
- Attach positive and negative rails nearest to J
DHT22 temperature and humidity sensor
- C30 to Pin 1 VCC (Power Supply)
- C29 to Pin 2 DATA (The data signal)
- C28 to Pin 3 NULL (Do not connect)
- C27 to Pin 4 GND (Ground)
- 10k ohm resistor (low precision) D30 to E29
- GND connection from E27 to negative rail nearest to J
- DATA connection from D29 to E21
- VCC connection from E30 to H4
Pololu 5V Step-Up Voltage Regulator U1V11F5
- Cut female header pins to 4 pin length
- Solder from pins F1 to F4
- Solder right angle male header pins to Regulator so board stands upright (supplied with regulator)
- F1 to shutdown (SHDN)
- F2 to input voltage (VIN) - from battery
- F3 to ground (GND)
- F4 to output voltage (VOUT) - 5V regulated
- 10k ohm pull-down resistor (low precision) J1 to negative rail nearest to J
- GND connection from J3 to negative rail nearest to J
- Electrolytic Decoupling Capacitor positive terminal G2
- Electrolytic Decoupling Capacitor negative terminal G3
- VIN (3.3V from LoPy4) from J12 to J2
DS18B20 external temperature sensor
- Cut female header pins to 3 pin length
- Solder from pins A1 to A3
- A1 to DATA
- A2 to VCC
- A3 to GND
- Solder right angle male header pins to temperature sensor leads
- GND connection from pin B3 to B27
- DATA connection from pin D1 to D23
- VCC (5V regulated supply) connection from pin E2 to I4
- 10k ohm pull-up resistor (low precision) from pin B1 to B2
MicroPython code for Pycom LoPy4 microcontroller
- There are several different code segments used on the Pycom LoPy4 microcontroller to monitor temperature.
- boot.py - bootstrap program that runs first.
- main.py - main program where program code is contained
- pymakr.conf - configuration file for Atom and Pymakr package
- lib - directory that contains additional library files
- config.py - list of static variables
- dth.py - library for DHT22 internal temperature sensor
- lora.py - library for LoRa. Contains private keys
- onewire.py - onewire library to communicate with DS18B20 external temperature sensor
boot.py
# boot.py -- run on boot-up
import pycom
from machine import UART
import machine
import os
from network import Bluetooth
from network import WLAN
if pycom.heartbeat() == True:
pycom.heartbeat(False)
if pycom.wifi_on_boot() == True:
pycom.wifi_on_boot(False)
#wlan = WLAN()
#wlan.deinit()
bluetooth = Bluetooth()
bluetooth.deinit()
uart = UART(0, baudrate=115200)
os.dupterm(uart)
machine.main('main.py')
main.py
pymakr.conf
{
"address": "COM10",
"username": "micro",
"password": "python",
"sync_folder": "",
"sync_file_types": [
"py",
"txt",
"log",
"json",
"xml",
"html",
"js",
"css",
"mpy",
"pem",
"cet",
"crt",
"key"
],
"sync_all_file_types": false,
"open_on_start": true,
"safe_boot_on_upload": false,
"py_ignore": [],
"fast_upload": false
}
config.py
dth.py
# dth.py
import time
import pycom
from machine import enable_irq, disable_irq, Pin
# DHT22 sensor pins - when sensor lying down
#1 - VCC - red wire Connect to 3.3 - 5V power. Sometime 3.3V power isn't enough in which case try 5V power.
#2 - Data out - white or yellow wire
#3 - Not connected
#4 - Ground - black wire
# Note - 10 Kohm resistor between VCC and the data pin
# current draw 1.5mA - pycom advise up to 6mA per pin
class DTHResult:
'DHT sensor result returned by DHT.read() method'
ERR_NO_ERROR = 0
ERR_MISSING_DATA = 1
ERR_CRC = 2
error_code = ERR_NO_ERROR
temperature = -1
humidity = -1
def __init__(self, error_code, temperature, humidity):
self.error_code = error_code
self.temperature = temperature
self.humidity = humidity
def is_valid(self):
return self.error_code == DTHResult.ERR_NO_ERROR
class DTH:
'DHT sensor (dht11, dht21,dht22) reader class for Pycom'
#__pin = Pin('P3', mode=Pin.OPEN_DRAIN)
__dhttype = 0
def __init__(self, pin, sensor=0):
self.__pin = Pin(pin, mode=Pin.OPEN_DRAIN)
self.__dhttype = sensor
self.__pin(1)
time.sleep(1.0)
def read(self):
# pull down to low
self.__send_and_sleep(0, 0.019)
data = pycom.pulses_get(self.__pin,100)
self.__pin.init(Pin.OPEN_DRAIN)
self.__pin(1)
#print(data)
bits = []
for a,b in data:
if a ==1 and 18 <= b <= 28:
bits.append(0)
if a ==1 and 65 <= b <= 75:
bits.append(1)
#print("longueur bits : %d " % len(bits))
if len(bits) != 40:
return DTHResult(DTHResult.ERR_MISSING_DATA, 0, 0)
#print(bits)
# we have the bits, calculate bytes
the_bytes = self.__bits_to_bytes(bits)
# calculate checksum and check
checksum = self.__calculate_checksum(the_bytes)
if the_bytes[4] != checksum:
return DTHResult(DTHResult.ERR_CRC, 0, 0)
# ok, we have valid data, return it
[int_rh, dec_rh, int_t, dec_t, csum] = the_bytes
if self.__dhttype==0: #dht11
rh = int_rh #dht11 20% ~ 90%
t = int_t #dht11 0..50°C
else: #dht21,dht22
rh = ((int_rh * 256) + dec_rh)/10
t = (((int_t & 0x7F) * 256) + dec_t)/10
if (int_t & 0x80) > 0:
t *= -1
return DTHResult(DTHResult.ERR_NO_ERROR, t, rh)
def __send_and_sleep(self, output, mysleep):
self.__pin(output)
time.sleep(mysleep)
def __bits_to_bytes(self, bits):
the_bytes = []
byte = 0
for i in range(0, len(bits)):
byte = byte << 1
if (bits[i]):
byte = byte | 1
else:
byte = byte | 0
if ((i + 1) % 8 == 0):
the_bytes.append(byte)
byte = 0
#print(the_bytes)
return the_bytes
def __calculate_checksum(self, the_bytes):
return the_bytes[0] + the_bytes[1] + the_bytes[2] + the_bytes[3] & 255
lora.py
# lora.py
import config
import utime
import time
from network import LoRa
import socket
import binascii
import struct
import machine
class LoraAU915: #Australia AU915
# https://stackoverflow.com/questions/5690888/variable-scopes-in-python-classes
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.AU915)
def setupLora():
#lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.AU915)
# Initialise LoRa in LORAWAN mode.
# create an OTAA authentication parameters, change them to the provided credentials
# whi-seedling-1
app_eui = binascii.unhexlify('YOUR_DEVICE_APP_EUI')
app_key = binascii.unhexlify('YOUR_DEVICE_APP_KEY')
#Limit channels for AU915
for i in range(0,8):
LoraAU915.lora.remove_channel(i)
for i in range(16,65):
LoraAU915.lora.remove_channel(i)
for i in range(66,72):
LoraAU915.lora.remove_channel(i)
# join a network using OTAA (Over the Air Activation)
#uncomment below to use LoRaWAN application provided dev_eui
#https://docs.pycom.io/tutorials/networks/lora/nvram/
new_connection = True
LoraAU915.lora.nvram_restore()
if(LoraAU915.lora.has_joined() == False):
print("LoRa memory restored but need to join LoRa")
LoraAU915.lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
else:
print("Memory restored and have rejoined LoRa with previous Dev Add")
new_connection = False
# wait until the module has joined the network
while not LoraAU915.lora.has_joined():
utime.sleep(1)
if utime.time() > 15:
print("possible timeout")
machine.reset()
pass
print('Joined')
# create a LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
if new_connection:
# set the LoRaWAN data rate
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 0) # dr0 equiv to SF12
# make the socket non-blocking
s.setblocking(False)
print("LoRa connection set up")
utime.sleep_ms(5)
return s
def saveLora():
LoraAU915.lora.nvram_save()
print("LoRa setting saved to memory")
onewire.py
Trouble shooting
- Ensure that all sensors have pull-up resistors connected to data lines.
- Ensure that the Pololu 5V regulator has a pull-down resistor connected to the shutdown (SHDN) pin.
- Ensure that the Pololu 5V regulator is plugged in correctly.
- Ensure that batteries used to power the circuit are fully charged and are in good health. The LoPy will not function correctly if batteries cannot deliver sufficient current on start up.
- Ensure that the Electrolytic capacitor is installed with the correct polarity.
- Ensure that the Pycom LoPy4 is plugged in correctly (LED closest to 5V regulator)




