The Parts Lists for the program shown below .
1)DS18B20 Temperature Sensor
4)several jumper wires
Wiring the DS18B20 Temperature sensor to the RPi
It is important to double check that you don’t confuse Pin 1 and Pin 3 on the device otherwise the power will be applied the wrong way round!Once you have connected everything together you can power up your Raspberry Pi.
Before we make the Python script we first need to setup the Pi so it can actually read data from the sensor. To do this we need to add OneWire support.
sudo nano /boot/config.txt
sudo modprobe w1-gpio sudo modprobe w1-therm
lsto see the folder/files in this directory.
cd /sys/bus/w1/devices ls
cdto what has appeared in your directory by using the
lscommand. (If you have multiple sensors there will be more than one directory)
Now it’s time to move onto the python script. Input following command.
git clone https://github.com/pimylifeup/temperature_sensor.git
To begin the python script, we import 3 packages, OS, Glob and time.
Next we run the modprobe commands these are the same as what we used before.
We then declare 3 different variables that will point to the location of our sensor data. You shouldn’t have to change any of these.
import os import glob import time os.system('modprobe w1-gpio') os.system('modprobe w1-therm') base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '28*') device_file = device_folder + '/w1_slave'
read_temp_raw function we open up the file that contains our temperature output. We read all the lines from this and then return it so the code that has called this function can use it. In this case the
read_temp function calls this function.
def read_temp_raw(): f = open(device_file, 'r') lines = f.readlines() f.close() return lines
read_temp function we process the data from the
read_temp_raw function. We first make sure that the first line contains YES. This means there will be a line with a temperature in it.
If there is a temperature we then find the line with
t= by using
lines.find('t='). Lines means we’re looking at the 2nd element in the array, in this case the 2nd line. Once we have the line we simply get all the numbers that are after the
t= this is done here
Equals_pos is the start position of the temperature (t) and we add 2 to the position so we only get the actual temperature numbers.
We then convert the number into both a Celsius and Fahrenheit temperature. We return both of these to the code that called this function. This is the print function located in the while loop.
def read_temp(): lines = read_temp_raw() while lines.strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines.find('t=') if equals_pos != -1: temp_string = lines[equals_pos+2:] temp_c = float(temp_string) / 1000.0 temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c, temp_f
The while loop is always true so it will run forever until the program is interrupted by an error or the user cancelling the script. It simply calls the read_temp within the print function. The print function allows us to see the output on our screen. The script is then put to sleep for 1 second every time it has read the sensor.
while True: print(read_temp()) time.sleep(1)
Once you have either downloaded or finished writing up the code and you also have setup the circuit correctly we can now call the Python script. To call the python script simply run the following command.
sudo python thermometer_sensor.py
You should now have an output of temperatures in both Fahrenheit and Celsius. You can alter this to just display your preferred temperature scale.
The programmable switch doesn’t required a lot of electronic components and is quite easy to build. The Parts Lists for the programmable switch is shown below .
1)tactile pushbutton switch
4)several jumper wires
In order to read the tactile pushbutton switch status, the RPi.GPIO library needs to be installed on the Raspberry Pi. The RPi.GPIO is a software module that conveniently allows the Raspberry Pi GPIO pins to be manipulated for electronic controls applications. To install the RPi.GPIO library onto the Raspberry Pi, open the LXTerminal and type the following linux installation command after the prompt:
pi@raspberrypi ~ $ sudo apt-get install python-dev python~rpi.gpio
After the linux installation command is entered, you will see a series of RPi.GPIO bulld-installation file sequences being displayed on the monitor as shown below.
Attaching a tactile pushbutton switch to the RPi is quite easy to do. An important item to remember is the RPi’s GPIO pins are +3.3VDC compliant. Applying voltages greater than +3.3VDC will damage the RPi. The electrical wiring diagram for attaching the tactile pushbutton switch is shown next.
Although, the circuit wiring to the RPi is quite simple, recheck the wiring before programming the RPi. This important verification step will assure you project execution success when the python code is installed and running on the RPi.
The next phase of the project build is to provide a python script for reading a tactile pushbutton switch wired to a RPi GPIO pin. The python script for reading a tactile pushbutton switch is shown next.
import RPi.GPIO as GPIO import time
GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_UP)
while True: inputValue = GPIO.input(18) if (inputValue == False): print("Button press ") time.sleep(0.3)
You can entered this script using either the LXTerminal’s nano editor or with the Python’s IDLE (Integrated Development Enviroment). Save the script as pbbutton.py in the home/pi directory of the RPi. Next, type the following linux command to run the script on the RPi into the LXTerminal as shown next.
Next, press the tactile pushbutton switch. If the script was typed correctly, you will see the message “button press” displayed on the monitor’s screen. Congratulations on building a programmable pushbutton switch!
pi@raspberrypi ~ $ sudo python pbbutton.py
The tactile pushbutton switch can easily be programmed to provide a variety of output messages and switching responses. Try changing the “Button Press” message to display your name or a whimiscal word when activating the switch. Record your results in a laboratory notebook.
To help beginners to easily study the Raspberry Pi hardware controlling, we designed some straight forward sample projects which can save user a lot of money and time to do research.