Print Friendly, PDF & Email

Difficulty level: Beginner

Approx reading time:

Components Required:

1. Raspberry Pi 3 model B

2. MicroSD card 8 or 16 GB (Class 4 and above) with Raspbian

3. Windows PC / Linux PC (Tested on Windows 10, Ubuntu 14.04 LTS)

4. Ethernet cable (Category 5 – also called Cat 5)

5. Micro USB cable

6. Breadboard

7. MCP3008 Analog to Digital Converter – 1

8. LM35 Temperature Sensor – 1

9. Jumper Wires (Male to Female, Male to Male)

Way to go ->

1. Login to your Pi using PuTTy or your Ubuntu terminal.

2. Login to your Pi GUI using VNC server (Linux / Windows).

3. In Python 3 (IDLE), create a new file.

4. The name could be: LM35Read.py.

5. In the file LM35Read.py, write the following code with comments (line starting with “#”) for clear understanding and save (press Cntrl + S on your PC keyboard) the file.

import spidev
from time import sleep


# First open up SPI bus
spi = spidev.SpiDev()
spi.open(0, 0)

# Initialize what sensor is where
tempChannel = 1
sleepTime = 1


def getReading(channel):
    # First pull the raw data from the chip
    rawData = spi.xfer([1, (8 + channel) << 4, 0])
    # Process the raw data into something we understand.
    processedData = ((rawData[1] & 3) << 8) + rawData[2]
    return processedData


def convertVoltage(bitValue, decimalPlaces=2):
    voltage = (bitValue * 3.3) / float(1023)
    voltage = round(voltage, decimalPlaces)
    return voltage


def convertTemp(bitValue, decimalPlaces=2):
    # Converts to degrees Celsius
    temperature = ((bitValue * 330) / float(1023))
    # 3300 mV / (10 mV/deg C) = 330
    temperature = round(temperature, decimalPlaces)
    return temperature

while (1):
    tempData = getReading(tempChannel)
    tempVoltage = convertVoltage(tempData)
    temperature = convertTemp(tempData)
    print ("Temp bitValue = {}; Voltage = {} V; Temp = {} C".format
           (tempData, tempVoltage, temperature))
    sleep(sleepTime)

Before running the python script do the following:

6. In the terminal type: sudo raspi-config and press Enter on your PC keyboard.

7. Go to Advanced Options.

7_AdvancedRaspi

8. Go to SPI.

8_SPI

9. Enable SPI Interface by clicking on <Yes>.

9_YesSPI

10. Reboot the Pi. In the terminal, type: sudo reboot

11. After it has rebooted, install Python 3 devlopment tools. In the terminal, type: 

sudo apt-get install python3-dev

12. Download this package. In the terminal type:

wget https://github.com/Gadgetoid/py-spidev/archive/master.zip

13. In the terminal type: ls to check that you have master.zip in your current working directory.

14. To unzip, in the terminal type: 

unzip master.zip

15. Remove master.zip: 

rm master.zip

16. Navigate to py-spidev-master directory(folder): 

cd py-spidev-master

17. Type in the terminal: ls, to view the contents of the directory(folder).

18. In the terminal run the command: 

sudo python3 setup.py install

19. Finally, run the script by clicking on Run -> Run Module in the menu bar or by pressing F5 on your PC keyboard.

19_Output

Hardware Connections ->

1. Raspberry Pi 3 GPIO Header.

pi3_gpio

2. MCP3008 to Pi connections.

MCPToPi

3. LM35 to Pi / MCP3008 connections.

LM35Conn

LM35Read_schem

 LM35Read_bb

Read full tutorial at

Rating widget: