0

members

Easy Project 19m Raspberry PI Zero W board - TMP36 sensor

of Acoptex.com in Raspberry Pi Zero W

Raspberry basics: Project 19m

Project name: Raspberry PI Zero W board - TMP36

Tags: Raspberry, Raspberry PI Zero W board, vers 1.1, v 1.1, TMP36, reading temperature, spidev library, time library

Attachments: tmp36.py

Raspberry Pi
Monitor or TV
HDMI cable
USB keyboard
USB mouse
Power supply
8GB SD card

In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):

1. Raspberry PI Zero W board 1 pc

2. Micro SD card with NOOBS and SD card adapter 1 pc

3. Micro USB power supply (2 A 5V or 5V 3A) 1 pc

4. USB keyboard 1 pc

5. USB mouse 1 pc

6. TV or PC monitor 1 pc

7. HDMI cable 1 pc

8. T-Cobbler Breakout and GPIO Cable 1 pc

9. Micro USB 2.0 OTG Cable 1 pc

10. Mini HDMI to HDMI Adapter (HDMI to Mini HDMI Adapter) 1 pc

11. 4-Port USB 2.0 Hub 1 pc

12. TMP36 sensor 1 pc

13. Jumper cables F-M, M-M

14. MCP 3008 ADC chip 1 pc

 

15. Breadboard 1 pc

General

We will learn how to use TMP36 to read the temperature with Raspberry PI Zero W board. 

Understanding the TMP36 sensor

You can read more about it here.

Understanding the MCP3008 chip

The Microchip Technology Inc. MCP3008 devices are successive approximation 10-bit Analog-to-Digital (A/D) converters with on-board sample and hold circuitry. The MCP3008 is programmable to provide four pseudo-differential input pairs or eight single-ended inputs. Differential Nonlinearity (DNL) and Integral Nonlinearity (INL) are specified at ±1 LSB. Communication with the devices is accomplished using a simple serial interface compatible with the SPI protocol. The devices are capable of conversion rates of up to 200 ksps. The MCP3008 devices operate over a broad voltage range (2.7V - 5.5V). Low-current design permits operation with typical standby currents of only 5 nA and typical active currents of 320 µA.  

Applications:

  • Sensor Interface
  • Process Control
  • Data Acquisition
  • Battery Operated Systems

You can find specification here.

Understanding the Raspberry PI Zero W board

You can read more about it here.

Signals and connections of the TMP36 sensor

Signals and connections of the MCP3008 chip

MCP3008 ADC will convert the potentiometer's analog signal to to digital signal.

MCP3008 ADC has a total of 16 pins out of which 8 pins are for taking the analog input. The analog input pins are from CH0-CH7 (Pins 1-8). On the other side, we have different pins which are as follows:

DGND is digital ground pin for the chip.

CS is the chip select pin.

DIN is the data input pin from the Raspberry Pi.

DOUT is the data output pin.

CLK is the clock pin.

AGND is the analog ground pin.

VREF is the analog reference voltage. Connect to 3.3V. You can change it if you want to change the scale.

VDD is the power pin for the chip (+2.7V to 5.5V)

Signals and connections of the Raspberry PI Zero W board


Wiring

Before applying power to the circuit make sure that you have connected the MCP3008 chip correctly or you can damage the chip.

To identify the MCP3008 pins, orient the chip so it’s facing you with the half-circle cutout at the top. The first pin is the top left and the last pin is the top right.

Step by Step instruction

We recommend using a high-performance SD card for increased stability as well as plugging your device into an external display to see the default application booting up.

1. Setup and preparation

We assume that you have Windows 10 installed on your PC and Raspbian OS installed on your Raspberry Pi Zero W board. 

  1. Do wiring.
  2. Insert your micro SD card with Raspbian OS into the TF card slot on the Raspberry Pi Zero W board. It will only fit one way.
  3. Connect Raspberry PI Zero W board mini HDMI port to your TV or Monitor HDMI (DVI) port (use HDMI cable and mini HDMI to HDMI adapter and/or HDMI to DVI adapter).
  4. Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI/DVI,  etc).
  5. Plug in micro USB 2.0 OTG Cable to USB data port of Pi Zero and 4-Port USB 2.0 Hub to micro USB 2.0 OTG Cable. 
  6. Plug in your USB mouse and USB keyboard to 4-Port USB 2.0 Hub.
  7. If you intend to connect your Raspberry Pi Zero vers 1.2 or vers 1.3 to the internet, connect a WiFi dongle to one of the 4-Port USB 2.0 Hub ports.
  8. Connect Micro USB power supply to Raspberry PI Zero board micro USB input.
  9. The Raspberry PI desktop will start up.
  10. Open Terminal window and type the command: sudo apt-get update
  11. Then type the command: sudo apt-get install 

2. Installing the spidev library

  1. Type the command in the Terminal window: pip install spidev

  2. Library succesfully installed.

3. Making the program

  1. The Raspberry Pi reads the analog values from the MCP3008 ADC chip using SPI communication, so we need to enable that first. Open the Raspberry Pi Configuration tool from the main menu. Go to Raspberry icon-> Preferences -> Raspberry PI configuration

  2. Select Interfaces and make sure that the SPI is enabled. If it’s not enabled, enable it and reboot your Raspberry PI 3 board to begin. We suggest to enable all settings and you will not need to come back to these configuration tool again.

  3. Click on OK button.
  4. We need to reboot our Raspberry PI. Go to Terminal. Type this command in the Terminal: sudo reboot
  5. After restart of Raspberry Pi go to Terminal. Type this command in the Terminal: sudo nano tmp36.py

  6. Copy and paste the code from tmp36.py to opened window. Since it is a Python code, you need to be careful with the Tab characters as it is important to group the instruction as blocks in Python.

  7. Press Ctrl+X, Y, Enter buttons to save the file.
  8. Type the command in the Terminal: sudo python tmp36.py

  9. You will see the TMP36 sensor value printed in the Terminal.

Code

The code is well explaned. Please check the comments in the code.

 r = spi.xfer2([1, 8 + adcnum << 4, 0]). This sends 3 bytes to the device. The first byte is 1 which is equal to 00000001 in binary.

“8+channel” is 00001000 in binary (where channel is 0). “<<4” shifts those bits to the left which gives 10000000. The last byte is 0 which is 00000000 in binary.

So “spi.xfer2([1,(8+channel)<<4,0])” sends 00000001 10000000 00000000 to the device. The device then sends back 3 bytes in response. The “data=” line extracts 10 bits from that response and this represents the measurement.

The exact reason why you do the above is explained in the datasheet.

Summary

We have learnt how to use TMP36 to read the temperature with Raspberry PI Zero W board. 

Libraries in use

  • spidev
  • time

Script

  • Look for attached code on the begining of this project


Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

Published at 30-12-2018
Viewed: 2426 times