A Python program can be used to read temperature and humidity sensor. In order to do, I adopted the following procedure.


  1. Temperature and Humidity Sensor(DHT 11)
  2. Breadboard
  3. Raspberry PI
  4. Jumper wires
  5. 10K ohm resistor

Circuit Diagram:


Physical Experiment:



Python Program to get values from sensor:

import sys

import Adafruit_DHT
# Parse command line parameters.
sensor_args = { ’11’: Adafruit_DHT.DHT11,
’22’: Adafruit_DHT.DHT22,
‘2302’: Adafruit_DHT.AM2302 }

if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
sensor = sensor_args[sys.argv[1]]
pin = sys.argv[2]
print(‘usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#’)
print(‘example: sudo ./Adafruit_DHT.py 2302 4 – Read from an AM2302 connected to GPIO #4’)

# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32

# Note that sometimes you won’t get a reading and
# the results will be null (because Linux can’t
# guarantee the timing of calls to read the sensor).
# If this happens try again!
if humidity is not None and temperature is not None:
print(‘Temp={0:0.1f}* Humidity={1:0.1f}%’.format(temperature, humidity))
print(‘Failed to get reading. Try again!’)

Program Explanation:

As per the conditional statement IF LEN(SYS.ARGV)==3, In order to run the program, we need to give three arguments in the command prompt.

  1. Name of the python file( a common need)
  2. Version of the DHT( specifically 11, 22, and 2302)
  3. Port to which data of sensor is connected

An Example command to get code executed:

sudo ./AdafruitDHT.py 2302 04

In AdafruitDHT package retry method is useful to fetch both humidity and temperature.

Now, we need to fetch results periodically. For example, let us suppose for each and every minute.In order to do that second part of the program must be placed in an infinite loop. And there must be a statement informing program to sleep for 60 seconds.


$sudo ./AdafruitDHT.py 11 04


SPI Interface of Raspberry Pi:

The Raspberry Pi is equipped with one SPI bus that has 2 chip selects.

The SPI master driver is disabled by default on Raspbian. To enable it, use raspi-config, or ensure the line dtparam=spi=on isn’t commented out in /boot/config.txt, and reboot. If the SPI driver was loaded, you should see the device /dev/spidev0.0.


SPI master slave architecture:

Standard Mode:

In Standard SPI master mode the peripheral implements the standard 3 wire serial protocol (SCLK, MOSI and MISO).

Bidirectional Mode:

In bidirectional SPI master mode the same SPI standard is implemented except that a single wire is used for data (MIMO) instead of two as in standard mode (MISO and MOSI).

LoSSI Mode:

The LoSSI standard allows issuing of commands to peripherals (LCD) and to transfer data to and from them. LoSSI commands and parameters are 8 bits long, but an extra bit is used to indicate whether the byte is a command or parameter/data. This extra bit is set high for a data and low for a command. The resulting 9-bit value is serialized to the output.


Reference for SPI: SPI Interface for PI