Raspberi Pi and 18d20

  • Reading the temperature from a 18d20 temperature sensor with Raspberry Pi is can not be easier
  • The next code it will allow you to just print at the terminal the data about the sensor and the temperature
  • Before you start using the 18b20 the 1wire buss need to be activated. Open a new terminal window and type:
sudo raspi-config
  • Use arrows and tab keys to navigate on the menu select Interfaces Options and then enable 1-Wire buss
  • now install the needed packages
pip install w1thermsensor
sudo apt-get install python3-w1thermsensor
  • And now at the pin GPIO 04 the 18b20 can be connected and the 4.7k resistor to the 5v dc
  • Now a new python3 cod can be created
sudo nano temperature.py
  • the following code will just read and print to the terminal the sensor info and the temperature
from w1thermsensor import W1ThermSensor
    #Read the data from the sensor
TempSensor = W1ThermSensor()
    #Print sensor info
print ("the sensor info=  ", TempSensor)
    #Read the temperature
temperature = TempSensor.get_temperature()
    #Print the temperature in Celsius, Fahrenheit and Kelvin
print("temperature in Celsius = ", temperature)
fahrenheit = (temperature*9/5)+32
print("temperature in Fahrenheit = ", fahrenheit)
kelvin = temperature+273.15
print("temperature in Kelvin = ", kelvin)
18b20 temp read code python