ModMyPi LTD | GPIO & Python (7/9)

von Satoshi Nakamoto

ModMyPi LTD | GPIO & Python (7/9)

On this mission you'll discover ways to wire and program a temperature sensor. Let’s see how scorching the room is.

Issues you will have:

Raspberry Pi + SD Card
Keyboard + Mouse
Monitor + HDMI Cable
Energy Provide
Breadboard
1x Crimson LED
1x Blue LED
2x 330Ω Resistor
5x M/M Jumper Wire
7x M/F Jumper Wire
1x Button
1x Buzzer
1x DS18B20 Temperature Sensor
1x 4k7Ω Resistor

Conditions:

Newest model of Rasbian put in in your SD Card
Raspberry Pi setup with a keyboard, mouse and monitor

1. First we have to allow I2C on our Raspberry Pi. We do that by opening
the raspi-config menu.


sudo raspi-config

2. We then must go to the Interfacing Choices menu.

3. Choose I2C after which Sure to allow it.

4. Subsequent we have to allow the 1-wire I2C library. To do that we have to edit the boot config.txt file


sudo nano /boot/config.txt

5. Scroll all the way down to the underside of the file and add the next line:


dtoverlay=w1-gpio

6. Now reboot


sudo reboot

7. Change the present listing to our gpio_python_code listing:


cd gpio_python_code

8. Create a file for our temperature script:


contact 7_temperature.py

9. Edit the 7_temperature.py script utilizing nano 7_temperature.py and add the next code:


#!/usr/bin/python

import glob
from time import sleep

base_dir = '/sys/bus/w1/units/'
device_folder = glob.glob(base_dir + '28*')
device_file = device_folder + '/w1_slave'

def read_temp_raw():
f = open(device_file, 'r')
strains = f.readlines()
f.shut()
return strains

def read_temp():
strains = read_temp_raw()
whereas strains.strip() != 'YES':
sleep(0.2)
strains = read_temp_raw()
equals_pos = strains.discover('t=')
if equals_pos != -1:
temp_string = strains
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f

whereas True:
print(read_temp())
sleep(1)

10. Execute your 7_temperature.py script


sudo python 7_temperature.py




Source link



Read the full article
Porträt von Satoshi Nakamoto

Satoshi Nakamoto

Zur Person

Satoshi Nakamoto