TP Guidé : Mesure de Température avec Raspberry Pi, MCP3008 et
Afficheur LCD sous Proteus
Objectif du TP
Réaliser un système de mesure de température utilisant :
Raspberry Pi 4 Model B
Convertisseur analogique MCP3008
Capteur de température TMP36
Écran LCD 16x2
Communication SPI
Simulation sous Proteus
Le système doit :
Lire la température depuis le capteur TMP36
Convertir la valeur analogique via MCP3008
Afficher la température sur LCD 16x2
1. Matériel Utilisé
Composant Quantité
Raspberry Pi 4
1
Model B
MCP3008 1
TMP36 1
LCD 16x2 1
Quelque
Résistances
s
Breadboard 1
Plusieur
Fils de connexion
s
Proteus 1
2. Principe de Fonctionnement
Le TMP36 délivre une tension proportionnelle à la température.
Comme le Raspberry Pi ne possède pas d’entrée analogique, on utilise le
MCP3008 :
TMP36 → MCP3008
MCP3008 → SPI Raspberry Pi
Raspberry Pi traite les données
LCD affiche la température
3. Architecture du Système
TMP36 → MCP3008 → Raspberry Pi → LCD 16x2
4. Schéma de Câblage
A. Connexion MCP3008 avec Raspberry Pi
MCP300 Raspberry
8 Pi
VDD 3.3V
VREF 3.3V
AGND GND
DGND GND
CLK SCLK
DOUT MISO
DIN MOSI
CS CE0
B. Connexion TMP36
TMP3 MCP300
6 8
VCC 3.3V
TMP3 MCP300
6 8
OUT CH0
GND GND
C. Connexion LCD 16x2
GPIO
LCD
Raspberry
RS Pin 15
E Pin 16
D4 Pin 7
D5 Pin 11
D6 Pin 12
D7 Pin 13
5. Réalisation sous Proteus
Étape 1 : Ajouter les composants
Dans Proteus :
Ajouter :
Raspberry Pi
MCP3008
TMP36
LCD 16x2
Sources d’alimentation
Étape 2 : Réaliser le câblage
Connecter :
TMP36 → MCP3008
MCP3008 → SPI Raspberry Pi
LCD → GPIO Raspberry
Étape 3 : Charger le programme Python
Le programme sera exécuté sur le Raspberry Pi simulé.
6. Explication du Programme Python
Le programme réalise :
1. Initialisation SPI
2. Initialisation LCD
3. Lecture analogique du TMP36
4. Conversion en température
5. Affichage LCD
7. Programme Python
#!/usr/bin/python
import spidev
import time
import os
import [Link] as GPIO
[Link]([Link])
[Link](False)
# Open SPI bus
spi = [Link]()
[Link](0,0)
# Define GPIO to LCD mapping
LCD_RS = 15
LCD_E = 16
LCD_D4 = 7
LCD_D5 = 11
LCD_D6 = 12
LCD_D7 = 13
# Define sensor channels
temp_channel = 0
'''
define pin for lcd
'''
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
delay = 1
[Link](LCD_E, [Link]) # E
[Link](LCD_RS, [Link]) # RS
[Link](LCD_D4, [Link]) # DB4
[Link](LCD_D5, [Link]) # DB5
[Link](LCD_D6, [Link]) # DB6
[Link](LCD_D7, [Link]) # DB7
# Define some device constants
LCD_WIDTH = 16 # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
'''
Function Name :lcd_init()
Function Description : this function is used to initialized lcd by sending the
different commands
'''
def lcd_init():
# Initialise display
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font
size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
[Link](E_DELAY)
'''
Function Name :lcd_byte(bits ,mode)
Fuction Name :the main purpose of this function to convert the byte data
into bit and send to lcd port
'''
def lcd_byte(bits, mode):
# Send byte to data pins
# bits = data
# mode = True for character
# False for command
[Link](LCD_RS, mode) # RS
# High bits
[Link](LCD_D4, False)
[Link](LCD_D5, False)
[Link](LCD_D6, False)
[Link](LCD_D7, False)
if bits&0x10==0x10:
[Link](LCD_D4, True)
if bits&0x20==0x20:
[Link](LCD_D5, True)
if bits&0x40==0x40:
[Link](LCD_D6, True)
if bits&0x80==0x80:
[Link](LCD_D7, True)
# Toggle 'Enable' pin
lcd_toggle_enable()
# Low bits
[Link](LCD_D4, False)
[Link](LCD_D5, False)
[Link](LCD_D6, False)
[Link](LCD_D7, False)
if bits&0x01==0x01:
[Link](LCD_D4, True)
if bits&0x02==0x02:
[Link](LCD_D5, True)
if bits&0x04==0x04:
[Link](LCD_D6, True)
if bits&0x08==0x08:
[Link](LCD_D7, True)
# Toggle 'Enable' pin
lcd_toggle_enable()
'''
Function Name : lcd_toggle_enable()
Function Description:basically this is used to toggle Enable pin
'''
def lcd_toggle_enable():
# Toggle enable
[Link](E_DELAY)
[Link](LCD_E, True)
[Link](E_PULSE)
[Link](LCD_E, False)
[Link](E_DELAY)
'''
Function Name :lcd_string(message,line)
Function Description :print the data on lcd
'''
def lcd_string(message,line):
# Send string to display
message = [Link](LCD_WIDTH," ")
lcd_byte(line, LCD_CMD)
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)
# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
# Function to calculate temperature from
# TMP36 data, rounded to specified
# number of decimal places.
def ConvertTemp(data,places):
# ADC Value
# (approx) Temp Volts
# 0 -50 0.00
# 78 -25 0.25
# 155 0 0.50
# 233 25 0.75
# 310 50 1.00
# 465 100 1.50
# 775 200 2.50
# 1023 280 3.30
temp = ((data * 330)/float(1023))
temp = round(temp,places)
return temp
# Define delay between readings
delay = 5
lcd_init()
lcd_string("welcome ",LCD_LINE_1)
[Link](2)
while 1:
temp_level = ReadChannel(temp_channel)
temp = ConvertTemp(temp_level,2)
# Print out results
lcd_string("Temperature ",LCD_LINE_1)
lcd_string(str(temp),LCD_LINE_2)
[Link](1)
8. Résultats Attendus
Température Affichage
TMP36 LCD
25°C 25.00 C
30°C 30.00 C
40°C 40.00 C
9. Analyse du Fonctionnement
MCP3008
Le MCP3008 convertit :
signal analogique
→ signal numérique SPI
TMP36
Le TMP36 fournit :
∘
V out =10 mV ¿ C
SPI
Le protocole SPI utilise :
MOSI
MISO
SCLK
CE
pour communiquer avec le MCP3008.