16
Large Pi-based Thermometer and Clock Created by Simon Monk Last updated on 2017-09-12 03:11:01 PM UTC

Large Pi-based Thermometer and Clock - Adafruit

  • Upload
    dangthu

  • View
    219

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Large Pi-based Thermometer and Clock - Adafruit

Large Pi-based Thermometer and ClockCreated by Simon Monk

Last updated on 2017-09-12 03:11:01 PM UTC

Page 2: Large Pi-based Thermometer and Clock - Adafruit

2344455667

813131316

Guide Contents

Guide ContentsOverviewParts

Raspberry Pi 3 - Model B - ARMv8 with 1G RAMAdafruit 1.2" 4-Digit 7-Segment Display w/I2C Backpack - RedAdafruit DS1307 Real Time Clock Assembled Breakout BoardDS18B20 Digital temperature sensor + extrasAdafruit Pi Cobbler + Kit- Breakout Cable for Pi B+/A+/Pi 2/Pi 3Premium Male/Male Jumper Wires - 20 x 3" (75mm)Half-size breadboard

HardwareSoftwareAdd 1-Wire to your PiInstall LED Backpack softwareConfigure and Test

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 2 of 16

Page 3: Large Pi-based Thermometer and Clock - Adafruit

OverviewThis project combines a whole heap of modules to enable a Raspberry Pi to power a large1.2 inch 4 digit 7 segment display. A small switch switches the display between showingthe temperature and the current time. The project uses a real-time clock (RTC) to ensurethat the Pi always has the correct time, even if it is not connected to the Internet.

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 3 of 16

Page 4: Large Pi-based Thermometer and Clock - Adafruit

Parts

Raspberry Pi 3 - Model B - ARMv8 with 1G RAM

PRODUCT ID: 3055Did you really think the Raspberry Pi would stop getting better? At this point, we sound likea broken record, extolling on the new Pi’s myriad improvements like we’re...http://adafru.it/scY$35.00IN STOCK

Adafruit 1.2" 4-Digit 7-Segment Display w/I2C Backpack - Red

PRODUCT ID: 1270What's better than a single LED? Lots of LEDs! A fun way to make a numeric display is touse a 4-digit 7-segment display. LED matrices like these are 'multiplexed' - so to...

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 4 of 16

Page 5: Large Pi-based Thermometer and Clock - Adafruit

http://adafru.it/scZ$17.50OUT OF STOCK

Adafruit DS1307 Real Time Clock Assembled Breakout Board

PRODUCT ID: 3296This is a great battery-backed real time clock (RTC) that allows your microcontroller projectto keep track of time even if it is reprogrammed, or if the power is lost. Perfect for...http://adafru.it/sd0$7.50IN STOCK

DS18B20 Digital temperature sensor + extras

PRODUCT ID: 374These 1-wire digital temperature sensors are fairly precise (±0.5°C over much of therange) and can give up to 12 bits of precision from the onboard digital-to-analog...http://adafru.it/sd1$3.95

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 5 of 16

Page 6: Large Pi-based Thermometer and Clock - Adafruit

IN STOCK

Adafruit Pi Cobbler + Kit- Breakout Cable for Pi B+/A+/Pi 2/Pi 3

PRODUCT ID: 1990The Raspberry Pi B+ has landed on the Maker World like a 40-GPIO pinned, quad-USBported, credit card sized bomb of DIY joy. And while you can use most of our great Model Baccessories by...http://adafru.it/sd2$6.50OUT OF STOCK

Premium Male/Male Jumper Wires - 20 x 3" (75mm)

PRODUCT ID: 1956Handy for making wire harnesses or jumpering between headers on PCB's. Thesepremium jumper wires are 3" (75mm) long and come in a 'strip' of 20 (2 pieces of each often rainbow...http://adafru.it/sd3$1.95

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 6 of 16

Page 7: Large Pi-based Thermometer and Clock - Adafruit

IN STOCK

Half-size breadboard

PRODUCT ID: 64This is a cute half size breadboard, good for small projects. It's 2.2" x 3.4" (5.5 cm x 8.5 cm)with a standard double-strip in the middle and two power rails on both...http://adafru.it/keP$5.00IN STOCK

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 7 of 16

Page 8: Large Pi-based Thermometer and Clock - Adafruit

HardwareThis project uses a lot of modules and there are a lot of connections to be made. The PiCobbler, LED Display and RTC are all supplied as kits that must be assembled.

You can follow the instructions on the product pages for the components, with oneexception, when you come to assemble the real-time clock module, do not solder the tworesistors R1 and R2. (Or if using the assembled version, cut the two jumper traces on thebottom) They are not needed when the module is used with a Raspberry Pi, as it has itsown pull-up resistors.

The RTC will allow a Raspberry Pi to know the time, even when not connected to theInternet. As such it is not essential to this project if your Raspberry Pi is going to have anInternet connection.

Having assembled the modules, you can then fit them onto the breadboard and wire them

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 8 of 16

Page 9: Large Pi-based Thermometer and Clock - Adafruit

up.

You can just about cram all the components onto a single half-sized breadboard, but it iseasier if you use two clipped together side to side.

The first step is to place all the components as shown below. Make sure everything is theright way around, especially the temperature sensor.

The diagram above shows the layout for a smaller 0.56 inch display which will work just aswell, however, in the final design, you will notice that just underneath the + connection onthe display, there is an orange jumper wire connected to +3V. The larger display needs thisextra connection to set the logic level to use, whereas the smaller 0.56 inch module doesnot. Otherwise, the connections are identical and either module can be used.

The next step is to make all the power connections. The diagram show GND connections inblue, +5V in red and +3.3V in purple.

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 9 of 16

Page 10: Large Pi-based Thermometer and Clock - Adafruit

Now lets add the leads for the I2C bus connections. I have used orange wires for SDA andyellow for SCA.

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 10 of 16

Page 11: Large Pi-based Thermometer and Clock - Adafruit

Finally we need the connections to the switch and the temperature sensor output, whichare made using green wires.

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 11 of 16

Page 12: Large Pi-based Thermometer and Clock - Adafruit

You may also like to look at separate tutorials for the RTC http://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi (http://adafru.it/aPm) and temperature sensorhttp://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing (http://adafru.it/aWY)

When everything is assembled, you can connect the ribbon cable to the GPIO connector.Remember to have the red band of the ribbon cable towards the SD card on the RaspberryPi.

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 12 of 16

Page 13: Large Pi-based Thermometer and Clock - Adafruit

Software

Add 1-Wire to your PiYou'll need to start by installing support for the DS18B20 1-Wire subsystem.

Visit this page to do so, you'll have to add an overlay to config.txt! (http://adafru.it/sd4)

Install LED Backpack softwareThe software for this project uses the Adafruit code for driving the 7 segment display. So,you should start by downloading this onto your Raspberry Pi.

sudo apt-get updatesudo apt-get install -y git build-essential python-dev python-smbus python-imaging python-pip python-pilgit clone https://github.com/adafruit/Adafruit_Python_LED_Backpack.git

We are going to place the program for this into a file inside the Adafruit code, so issue thefollowing commands to get to the right directory.

cd Adafruit_Python_LED_Backpacksudo python setup.py install

Now create a new file to edit, by typing the command:

nano thermo_clock.py

Then paste the program below into the editor.

import osimport globimport timeimport datetimefrom Adafruit_LED_Backpack import SevenSegmentimport RPi.GPIO as ioimport subprocess

io.setmode(io.BCM)switch_pin = 18io.setup(switch_pin, io.IN)

segment = SevenSegment.SevenSegment(address=0x70)

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 13 of 16

Page 14: Large Pi-based Thermometer and Clock - Adafruit

# Initialize the display. Must be called once before using the display.segment.begin()

os.system('modprobe w1-gpio')os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'device_folder = glob.glob(base_dir + '28*')[0]device_file = device_folder + '/w1_slave'

def read_temp_raw(): catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out,err = catdata.communicate() out_decode = out.decode('utf-8') lines = out_decode.split('\n') return lines

def read_temp(): lines = read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][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

def display_temp(): segment.set_colon(False) temp = int(read_temp()[1]) # F # temp = int(read_temp()[0]) # C sign = (temp < 0) temp = abs(temp) digit_1 = temp % 10 temp = temp / 10 digit_2 = temp % 10 temp = temp / 10 digit_3 = temp % 10 if sign : segment.set_digit_raw(0, 0x40) # - sign if digit_3 > 0 : segment.set_digit(0, digit_3) # Hundreds else: segment.set_digit_raw(0, 0) if digit_2 > 0 : segment.set_digit(1, digit_2) # Tens else: segment.set_digit_raw(1, 0) segment.set_digit(2, digit_1) # Ones

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 14 of 16

Page 15: Large Pi-based Thermometer and Clock - Adafruit

segment.set_digit_raw(3, 0x71) #F # Temp units letter #segment.set_digit_raw(3, 0x39) #C

def display_time(): now = datetime.datetime.now() hour = now.hour minute = now.minute second = now.second # Set hours segment.set_digit(0, int(hour / 10)) # Tens segment.set_digit(1, hour % 10) # Ones # Set minutes segment.set_digit(2, int(minute / 10)) # Tens segment.set_digit(3, minute % 10) # Ones # Toggle colon segment.set_colon(second % 2) # Toggle colon at 1Hz

while True: segment.clear() if io.input(switch_pin): display_temp() else : display_time() segment.write_display() time.sleep(0.5)

Python is indent/whitespace sensitive! So make sure you get all the tabs/indents as shownabove. Press CTRL-X and then Y to save the file.

The code is really a merger of code from a number of other projects. The code fordisplaying the time comes from the file ex_7segment_clock.py in the same directory as thisprogram. The code for reading the temperature is taken from this tutorialhttp://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing (http://adafru.it/aWY)

The main loop simply checks the position of the switch and then either displays thetemperature or the time. You can also find information on using switches with the GPIOlibrary here: http://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement (http://adafru.it/c67)

© Adafruit Industries https://learn.adafruit.com/large-pi-based-thermometer-and-clock Page 15 of 16

Page 16: Large Pi-based Thermometer and Clock - Adafruit

Configure and TestWe are now ready to run the program, but first if you are using the RTC, you need to set it.You can find instructions to do this here: http://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi (http://adafru.it/aPm)

To run the program type the following command:

sudo python thermo_clock.py

The temperature display can easily be changed from displaying degrees F to degrees C byswapping over a few lines in the code.

segment.set_digit_raw(3, 0x71) #F # Temp units letter#segment.set_digit_raw(3, 0x39) #C

and

temp = int(read_temp()[1]) # F # temp = int(read_temp()[0]) # C

To swap, just move the # in front of the line that does not apply.

© Adafruit Industries Last Updated: 2017-09-12 03:10:59 PM UTC Page 16 of 16