ADVENTSKALENDER ADVENTSKALENDER FÜR FÜR RASPBERRY RASPBERRY ER RASPBERRY PI ERFÜR FÜR RASPBERRY PI ADVENT ADVENT CALENDAR CALENDAR FOR FOR RASPBERRY RASPBERRY P P R FOR AR FORRASPBERRY RASPBERRYPI PI 24 Experimente, die Spaß machen 24 thrilling experiments ADV
Overview of all experiments Raspberry Pi Advent Calendar 2019. . . . . . . . . . . . . . . 3 1st day. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 13th day. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2. day. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 14th day. . . . . . . . . . . . . . .
Raspberry Pi Advent Calendar 2019 This Advent calendar contains a hardware experiment with the Raspberry Pi for every day. The experiments are programmed using Scratch and Python. Both programming languages are pre-installed on the Raspberry Pi. All experiments work with Raspberry Pi 3, Raspberry Pi 3 B+ and Raspberry Pi 4 with the current version of the Raspbian operating system.
1st day 1.
3. Copy all files and subdirectories from NOOBS to the SD card. 4. Remove the SD card from the PC, insert it into Raspberry Pi and boot. Select English as the installation language at the bottom. This automatically selects the English keyboard as well. 5. Check the preselected Raspbian operating system and click Install at the top left. After confirming a security prompt that the memory card is being completely overwritten, the installation starts, which takes a few minutes. 6.
LEDs LEDs (short for light emission diodes) light up when a current flows through them in the direction of flow. LEDs are represented in circuits with an arrow-shaped triangle symbol, which indicates the flow direction from the positive pole to the negative pole or to the ground line. An LED lets almost any current through in the direction of flow; it has a very low resistance.
GPIO connection cable The coloured connection cables all have a plug on one side and a socket on the other side, which fits on a GPIO pin of the Raspberry Pi. The LEDs are also plugged directly into these sockets. The plugs are plugged into the plug-in board. The programmable GPIO pins on the Raspberry Pi have numbers, the ground pins are marked by GND in the figure. Assignment of the GPIO pins. This picture is included in the downloads for the Advent calendar.
Precautions You should under no circumstances connect any GPIO pins to each other and wait and see what happens. Not all GPIO pins can be programmed freely. A few are fixed for power supply and other purposes. Some GPIO pins are directly connected to the processor terminals, a short circuit can completely destroy the Raspberry Pi. If two pins are connected to each other via a switch or an LED, a protective resistor must always be connected in between. LEDs with built-in series resistors are an exception.
2rd day Today in the Advent calendar 2. Day • 1 LED green • 1 resistor 220 ohms • 1 GPIO connection cable Two LEDs flash alternately The experiment of the second day causes two LEDs to light up alternating between red and green. The whole thing is controlled via an endless loop in Scratch.
Switching Scratch 2 to English Scratch 2 offers support for various hardware components on the GPIO port, which must be activated once via an add-on module. To do this, click More blocks on the block palette, and then click Add extension. Select the extension Pi GPIO here and install by double clicking. Install Scratch 2 Pi-GPIO extension The program In Scratch you don't have to type any program code when programming. The blocks are simply attached to each other by dragging and dropping.
You can assemble the program on screen or use the program 02led02 from the download to the Advent calendar. To do this, from the File/Load Project menu, select the pi button in the next dialog box to select the personal home directory where the downloaded programs are located. To assemble the program yourself, click on the yellow Control icon at the top of the block palette in the middle of the Scratch window. The blocks for Control are then shown in the block palette.
3rd day 3. Day Today in the Advent calendar • 1 LED yellow • 1 resistor 220 ohms • 1 GPIO connection cable Traffic light circuit with Python A traffic light with its typical light cycle from green to yellow to red and then back to green via a light combination red-yellow is easy to set up with three LEDs and shows how to control the GPIO pins on the Raspberry Pi in the popular programming language Python.
The first launch of Mu. Extract from the Python flashcards The program Instead of slowing down with programming theory, algorithms and data types, we write the first small program in Python. In Mu's main window, type in the program code shown. You can see interactive help texts as you type. Mu recognises the words and standard functions of the Python programming language and displays tips for use.
Start the program with the symbol Execute. How it works It is easy to test whether the program works. Now, of course, some questions will arise: What happens in the background? What do the individual program lines mean? This first project shows the basic elements of Python and the RPi.GPIO library. #!/usr/bin/python Python programs that are started from the command line must always start with the top line. This is not necessary for programs that are only started via the Python shell.
print("Ctrl+C ends the program") Now a short operating manual is displayed on the screen. The program runs automatically. The key combination [Ctrl]+[C] should end it. To query whether the user quits the program with [Ctrl]+[C], we use a try...except query. The program code entered under try: is initially executed normally.
4th day 4. Day Today in the Advent calendar • Seven-segment display Today, the Advent calendar contains a four-digit seven-segment display with which numbers and simple symbols can be displayed. Such seven-segment displays are often used in digital clocks, and previously also in pocket calculators. Petrol stations use this display technology in a large format. However, mechanical display elements are often used here instead of LEDs.
Wiring diagram of the seven-segment display Once the seven-segment display is installed, the connections are difficult to see. It is installed on the plug-in board in rows 5 to 10 in all experiments in this Advent calendar. The following table shows which pins of the seven-segment display are connected to which GPIO pins. The grey pins are not used in this program. In this program only the three horizontal segments A, G and D are connected to GPIO pins via resistors.
5th day 5. Day Today in the Advent calendar • 1 GPIO connection cable Running light on the seven-segment display The experiment of the 5th day causes the middle segment of all four digits of the seven-segment display to light up as a running light. For this purpose, the program switches the cathodes of the digits alternately, while the anode for the segment always remains switched on.
6th day Today in the Advent calendar 6. Day • 3 GPIO connection cable Another running light on the seven-segment display The experiment of the 6th day causes all segments of the first digit of the seven-segment display to light up as a running light. This is possible with only one series resistor by means of a circuit trick.
except KeyboardInterrupt: GPIO.cleanup() How it works A variable of type dictionary, a special form of the list, is used for the seven segments of the display. In a dictionary , the individual elements are not selected by their number, but by any word, the so-called key, which can also consist of a single letter. In contrast to a simple list, a dictionary is enclosed in brackets. It can contain any number of pairs of key and value.
7th day Today in the Advent calendar 7. Day • 1 resistor 220 ohms • Jumper wire Jumper wire Today, jumper wire is included in the Advent calendar. The jumper wire is used to create short connection bridges, which are used to connect rows of contacts on the plug-in board. Depending on the experiment, cut the wire to the appropriate length using a small wire cutter.
The program The program 07seg7.py runs a running light on all four digits over the lower four segments (C, D, E, G). #!/usr/bin/python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) seg={'a':21, 'b':8, 'c':11, 'd':26, 'e':19, 'f':20, 'g':13} for s in "abcdefg": GPIO.setup(seg[s], GPIO.OUT, initial=0) digit=16 GPIO.setup(num, GPIO.OUT, initial=0) print("Strg+C ends the program") try: while True: for s in "cdeg": GPIO.output(seg[s], 1) time.sleep(0.1) GPIO.
8th day Today in the Advent calendar 8. Day • 3 resistors 220 ohms Controlling a seven-segment display with Scratch The program of the 8th day controls the segments of the seven-segment display interactively and individually with Scratch. New programming techniques from Scratch that have not yet been described are presented.
The first segment in Scratch 2‘s painting program Each figure in Scratch can change its appearance using so-called costumes. Copy the existing costume. To do this, right-click it in the list of costumes and select Duplicate from the context menu. Rename the two costumes: out for the turned off LED and on for the turned on LED. Edit costume one using the paint program. Fill the coloured area with the colour bucket icon with red to indicate the LED is on.
After all script blocks are finished, duplicate the figure A by right clicking the figure window. Rename the new figure to B. Rotate the new figure by 90°, as it represents the vertical segment B. Don't turn the costumes, turn the whole figure. Switch to the Scripts tab and enter 90 degrees on the Movement block palette in the Rotate clockwise ... on the block pallet. You do not need to drag the block into the program. Simply double click on it, it will be executed, and the figure rotated.
9th day 9. Day Today in the Advent calendar • 1 GPIO connection cable Digits on the seven-segment display The actual purpose of a seven-segment display in most cases is not graphic gimmickry, but to display numbers. The program of the 9th day shows how this works. The circuit design is the same as on the 8th day, but this time we use Python instead of Scratch. The program The ten digit images of a seven-segment display. The program 09zahl.
How it works In this program, the GPIO pins for the seven segments are also initialised using a dictionary. number=[ "abcdef", #0 "bc", #1 "abdeg", #2 "abcdg", #3 "bcfg", #4 "acdfg", #5 "acdefg," #6 "abc", #7 "abcdefg", #8 "abcdfg", #9 ] The list number defines the segments to be switched on for the individual digits from 0 to 9. This is a normal list, of which the elements are only written below each other for the sake of clarity. The digits themselves are not part of the list.
10th day 10. Day Today in the Advent calendar • 1 GPIO connection cable Multiple digits on the seven-segment display The previous experiment used all four digits of the seven-segment display simultaneously. It is much more interesting to use all four digits independently to represent different digits.
digit=[16, 12, 7, 6] for z in digit: GPIO.setup(z, GPIO.OUT, initial=1) number=[ "abcdef", #0 "bc", #1 "abdeg", #2 "abcdg", #3 "bcfg", #4 "acdfg", #5 "acdefg," #6 "abc", #7 "abcdefg", #8 "abcdfg", #9 ] print("Strg+C ends the program") try: while True: for i in range(4): for s in "abcdefg": GPIO.output(seg[s], 0) GPIO.output(digit[i], 0) for s in number[i+1]: GPIO.output(seg[s], 1) time.sleep(0.001) GPIO.output(digit[i], 1) except KeyboardInterrupt: GPIO.
11th day 11. Day Today in the Advent calendar • 1 GPIO connection cable The circuit design is the same as on the 10th day. Show arbitrary numbers In the last program, the number 1234 was fixed in the program. The next program offers the possibility to enter and display arbitrary numbers. The options for correcting user input errors are also shown. The program The program 11zahl.py requires a four-digit number, but this does not mean that the user actually enters it.
else: z[i] = 0 sek = time.time() while time.time() <= sec + 2: za() except KeyboardInterrupt: GPIO.cleanup() How it works Large parts of the program are known from the 10th day. Everything concerning the initialisation of the GPIO ports and the list for displaying the digits on the seven segments of the display is taken from the last program. z = [0,0,0,0] The list z[] contains four digits to be displayed in the course of the program.
s = s.zfill(4) To mitigate the problem that the user may enter fewer than four characters or even simply press the [Enter] key, the string is filled in with zeros down to four digits at the beginning. The character string s is therefore always at least four characters long. for i in range(4): Now a loop begins which determines a valid digit for each of the four digits of the display and writes it to the list z[]. The first four characters of the character string s are evaluated.
12th day Today in the Advent calendar 12. Day • 1 GPIO connection cable The circuit design is the same as on the last days. Countdown to Christmas The program for the 12th day indicates how many days there are left until Christmas on the seven-segment display. The program The program 12countdown.py uses some new Python elements for date calculation. #!/usr/bin/python import RPi.GPIO as GPIO import time, datetime GPIO.setmode(GPIO.
13th day 13. Day Today in the Advent calendar • 1 button • 1 resistor 10 kOhm The connection diagram of the seven-segment display is the same as for the last days. Connecting a push-button to a GPIO pin Not only can GPIO pins output data, for example via LEDs, they can also be used for data input. For this they must be defined as inputs in the program. In the next project, we will use a button for input, which is plugged directly into the plug-in board.
seg={'a':21, 'b':8, 'c':11, 'd':26, 'e':19, 'f':20, 'g':13} for s in "abcdefg": GPIO.setup(seg[s], GPIO.OUT, initial=0) digit=[16, 12, 7, 6] for z in digit: GPIO.setup(z, GPIO.OUT, initial=1) number=[ "abcdef", #0 "bc", #1 "abdeg", #2 "abcdg", #3 "bcfg", #4 "acdfg", #5 "acdefg," #6 "abc", #7 "abcdefg", #8 "abcdfg", #9 ] button=18 GPIO.setup(button, GPIO.IN, GPIO.PUD_DOWN) z=[0,0,0,0] def za(): for i in range(4): for s in "abcdefg": GPIO.output(seg[s], 0) GPIO.
How it works The button at pin 18 is defined as GPIO input. The pin number is stored in the variable button. button=18 GPIO.setup(button, GPIO.IN, GPIO.PUD_DOWN) The program again uses the function za() known from an earlier program to display a four-digit number on the seven-segment display. t=datetime.date(2019, 12, 24) - datetime.date.today() x=t.
14th day Today in the Advent calendar 14. Day • 1 button The connection diagram of the seven-segment display is the same as for the last days. Connecting push-buttons without external pull-down resistors Current Raspberry Pi models have built-in pull-down resistors that can be switched on and off on the software side. This saves the external resistor. The button can be connected directly between +3.3V and the GPIO pin, as explained in the experiment of the 14th day.
GPIO.setup(button2, GPIO.IN, GPIO.PUD_DOWN) z=[0,0,0,0] def za(): for i in range(4): for s in "abcdefg": GPIO.output(seg[s], 0) GPIO.output(digit[i], 0) for s in number[z[i]]: GPIO.output(seg[s], 1) time.sleep(0.001) GPIO.output(digit[i], 1) print("Press button 1 to start countdown and restart") print("Press 2 to quit") while True: t=datetime.date(2019, 12, 24) - datetime.date.today() x=t.days z[0]=int(x/1000) z[1]=int(x%1000/100) z[2]=int(x%100/10) z[3]=int(x%10) if GPIO.
How it works The initialisation of the GPIO pins and the list for displaying the digits on the seven segments of the display is taken from the last program. button1=18 GPIO.setup(button1, GPIO.IN, GPIO.PUD_DOWN) button2=4 GPIO.setup(button2, GPIO.IN, GPIO.PUD_DOWN) The GPIO pins of the two buttons are then initialised. The parameter GPIO.PUD_DOWN at the initialisation of the GPIO pins for the push buttons turns on the built-in pull-down resistor.
15th day 15. Day Today in the Advent calendar • 1 GPIO connection cable The connection diagram of the seven-segment display is the same as for the last days. Digits with Scratch on the seven-segment display The program of the 15th day shows how Scratch numbers can be displayed on a seven-segment display. Scratch is unfortunately not fast enough to control all four digits of the display by time division multiplex, even in turbo mode. The program therefore only displays a single digit at a time.
If the user has entered a 0... If the first digit is 0, GPIO pins 21, 8, 11, 26, 19, 20 will be switched on and GPIO pin 13 will be set to Low and switched off. Similar queries will follow for all other digits. All seven segments are always switched, not just the required ones, so that none of the last digits displayed remains. The Scratch cat asks for a number Duplicate blocks When building a Scratch program, you do not need to create similar block combinations every time.
16th day 16. Day Today in the Advent calendar • 1 resistor 220 ohms Digital clock In the experiment of the 16th day, we build a working digital clock that displays the current time on the seven-segment display. The digital clock can run automatically on the Raspberry Pi without the monitor and keyboard being connected.
"abdeg", #2 "abcdg", #3 "bcfg", #4 "acdfg", #5 "acdefg," #6 "abc", #7 "abcdefg", #8 "abcdfg", #9 ] z = [0,0,0,0] print("Strg+C ends the program") def za(): for i in range(4): for s in "abcdefg": GPIO.output(seg[s], 0) GPIO.output(digit[i], 0) for s in number[z[i]]: GPIO.output(seg[s], 1) if i == 1: GPIO.output(dp, 1) else: GPIO.output(dp, 0) time.sleep(0.001) GPIO.output(digit[i], 1) try: while True: time = time.localtime() h = time.tm_hour m = time.
try: while True: time = time.localtime() In each run, the current time is written to object time, regardless of how long it lasts. The function time. localtime() from the time library is used for this purpose. The result is a data structure that consists of different single values. h = time.tm_hour m = time.tm_min The two values relevant for the digital clock, hours and minutes, are written from the structure into the variables h and m.
17th day Today in the Advent calendar 17. Day • 1 GPIO connection cable The connection diagram of the seven-segment display is the same as on the previous day, although the number on the far right is not used. Show IP address of Raspberry Pi The person operating a Raspberry Pi without keyboard and monitor only over the network, requires its IP address in order to establish the SSH connection. This address can be found using network scanner software.
"abc", #7 "abcdefg", #8 "abcdfg", #9 ] z = [0,0,0] ip = os.popen("hostname -I").readline()[:-2].split(".") print("Ctrl+C ends the program") def za(): for i in range(3): for s in "abcdefg": GPIO.output(seg[s], 0) GPIO.output(digit[i], 0) for s in number[z[i]]: GPIO.output(seg[s], 1) time.sleep(0.001) GPIO.output(digit[i], 1) for s in "abcdefg": GPIO.output(seg[s], 0) for k in range(3): GPIO.output(digit[k], 0) GPIO.output(dp, 1) time.sleep(0.5) GPIO.
def blink(): for s in "abcdefg": GPIO.output(seg[s], 0) for k in range(3): GPIO.output(digit[k], 0) GPIO.output(dp, 1) time.sleep(0.5) GPIO.output(dp, 0) In addition, a function blink() is defined, which flashes the three decimal points of the digits used for 0.5 seconds. First all seven segments are set to 0 and thus switched off. The cathodes of the first three digits are then set to 0 and the decimal point to 1. Now the three decimal points will light up. After a waiting time of 0.
18th day 18. Day Today in the Advent calendar • 1 button The connection diagram of the seven-segment display is the same as for the previous days. Stopwatch The experiment of the 18th day is a stopwatch with three buttons. The first button starts the stopwatch, the second stops to the nearest tenth of a second and the third resets the measured time to 0.
def za(): for i in range(4): for s in "abcdefg": GPIO.output(seg[s], 0) GPIO.output(digit[i], 0) for s in number[z[i]]: GPIO.output(seg[s], 1) if i == 2: GPIO.output(dp, 1) else: GPIO.output(dp, 0) time.sleep(0.001) GPIO.output(digit[i], 1) try: while True: z = [0,0,0,0] while GPIO.input(t1)==0: za() start = time.time() while GPIO.input(t2)==0: time = time.time() - start z[0] = int(time % 1000 / 100) z[1] = int(time % 100 / 10) z[2] = int(time % 10) z[3] = int(time * 10 % 10) while int((time.
if i == 2: GPIO.output(dp, 1) else: GPIO.output(dp, 0) This function is slightly changed. Whenever the third digit lights up, the decimal point is also turned on. For the other digits it won't light up. start = time.time() When the user presses button 1, the first while loop ends. The current time in seconds is stored in the variable start. From this time, the current time of the stopwatch is always calculated, which was started by pressing button 1. The function time.
19th day Today in the Advent calendar 19. Day • 1 resistor 20 MOhm (red-black-blue) • 1 plasticine The connection diagram of the seven-segment display is the same as for the previous days. Touch sensor from plasticine Today traffic lights, door openers, light switches and machines are often controlled with sensor contacts that you only have to touch. Pushbuttons that really need to be pressed are becoming increasingly rare. The GPIO pin switched as input is connected to +3.
The program The program 19stoppuhr.py is similar to the program of the 18th day, with the difference that only one sensor contact is used for start, stop and reset. #!/usr/bin/python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) seg={'a':21, 'b':8, 'c':11, 'd':26, 'e':19, 'f':20, 'g':13} for s in "abcdefg": GPIO.setup(seg[s], GPIO.OUT, initial=0) digit=[16, 12, 7, 6] for z in digit: GPIO.setup(z, GPIO.OUT, initial=1) dp = 5 GPIO.setup(dp, GPIO.OUT, initial=0) k1=4 GPIO.setup(k1, GPIO.
z[1] = int(time % 100 / 10) z[2] = int(time % 10) z[3] = int(time * 10 % 10) while int((time.time() - start) * 10 % 10) == z[3]: za() while GPIO.input(k1)==0: pass while GPIO.input(k1)==1: za() while GPIO.input(k1)==0: pass except KeyboardInterrupt: GPIO.cleanup() How it works The program is based on known elements; the initialisation of the GPIO pins for the seven-segment display is also the same as for the previous days. k1=4 GPIO.setup(k1, GPIO.
20th day 20. Day Today in the Advent calendar • 1 resistor 20 MOhm (red-black-blue) The connection diagram of the seven-segment display is the same as for the previous days. Counter with sensor contacts The experiment of the 20th day is a simple counter on the seven-segment display controlled by two sensor contacts. One sensor contact counts up; the other counts down. The sensor contacts are connected to GPIO pins 17 and 4.
def za(n): z[0] = int(n / 1000) z[1] = int(n % 1000 / 100) z[2] = int(n % 100 / 10) z[3] = int(n % 10) for i in range(4): for s in "abcdefg": GPIO.output(seg[s], 0) GPIO.output(digit[i], 0) for s in number[z[i]]: GPIO.output(seg[s], 1) time.sleep(0.001) GPIO.output(digit[i], 1) try: while True: za(x) if GPIO.input(k1)==0 and x>0: x-=1 if GPIO.input(k2)==0 and x<9999: x+=1 except KeyboardInterrupt: GPIO.cleanup() How it works This program is also largely based on earlier programs.
The main loop of the program displays the current value of the counter x on the seven-segment display in each pass via function za(x). if GPIO.input(k1)==0 and x>0: x-=1 If the sensor contact k1 is touched and the counter is still greater than 0, it is reduced by 1. This ensures that the counter does not reach any negative values. if GPIO.input(k2)==0 and x<9999: x+=1 If the sensor contact k1 is touched and the counter is still smaller than 9999, it is increased by 1.
21th day Today in the Advent calendar 21. Day • Download code The connection diagram of the seven-segment display is the same as for the previous days. The Raspberry Pi produces tones The Raspberry Pi can play music through an HDMI monitor, external speaker or headphones on the 3.5 mm analogue jack. For computer monitors with a DVI connector connected to the HDMI output, a speaker must be connected to the analogue output because the audio signal is not transmitted via the DVI cable.
Each key now gets its own program blocks. These are similar for all keys and play a specific tone when the key is clicked. If you are assembling the program for the first key, you can duplicate it for the other keys and only need to change it slightly. The program blocks do not start when the green flag is clicked, but each time the corresponding key is clicked. Select the key C in the figure list and drag When I am clicked into the script window from the block palette Events.
Draw the first black key as a filled rectangle, slightly smaller than the white keys. Also use the mode Vector graphic. Slide this button between the first two white buttons so that the top edges are flush with each other. The program blocks of the black keys are largely the same as those of the white keys. The black keyboard keys must become a little brighter and not darker when you click on them, so that they are easy to recognise. Set the brightness to 40 instead of -10.
22th day 22. Day Today in the Advent calendar • 2 resistors 220 ohms The connection diagram of the seven-segment display is the same as for the previous days. The decimal point is not used. Traffic light with countdown The experiment of the 22nd day simulates a traffic light with three LEDs. During the green phase, a countdown shows how much time you have left.
A variable z is counted down in a countdown loop. It contains the number to be displayed in each run. Variables in Scratch Variables are small memory locations where a program remembers a number or something else. When the program is closed, these variable memories are automatically emptied. Variables have to be created in Scratch on the block palette Data by clicking on New variable before they can be used.
The new block on the block pallet In the main program, the countdown counter z is first set to 9 after the green LED of the traffic light has been turned on. A loop then starts, which runs until the counter z is smaller than 0. In each loop pass, the block digit ... is generated and the with the current number z of the countdown so that this number is displayed. After a waiting time of 0.2 seconds, the variable z is counted down by 1 and the loop starts the next pass.
The whole program at a glance
23th day 23. Day Today in the Advent calendar • 3 GPIO connection cable The connection diagram of the seven-segment display is the same as for the previous days. The decimal point is also used. Number rates with three keys This is a simple guessing game in which a random number chosen by the computer should be guessed by the player in as few steps as possible. The LEDs are connected to the GPIO pins 24 and 23, the buttons to the GPIO pins 18, 17 and 4.
LED1=24 GPIO.setup(LED1, GPIO.OUT, initial=0) LED2=23 GPIO.setup(LED2, GPIO.OUT, initial=0) t1=18 t2=17 t3=4 GPIO.setup(t1, GPIO.IN, GPIO.PUD_DOWN) GPIO.setup(t2, GPIO.IN, GPIO.PUD_DOWN) GPIO.setup(t3, GPIO.IN, GPIO.
def za(n): z[0] = int(n / 1000) z[1] = int(n % 1000 / 100) z[2] = int(n % 100 / 10) z[3] = int(n % 10) for i in range(4): for s in "abcdefg": GPIO.output(seg[s], 0) GPIO.output(digit[i], 0) for s in number[z[i]]: GPIO.output(seg[s], 1) if i == 1: GPIO.output(dp, 1) else: GPIO.output(dp, 0) time.sleep(0.001) GPIO.output(digit[i], 1) The function za(n) for displaying a four-digit number has a similar structure to the program of the 20th day.
• If the searched numbery is smaller than the given tip, both left LEDs illuminate for half a second. if y>x: GPIO.output(LED2, 1) time.sleep(0.5) GPIO.output(LED2, 0) • If the searched numbery is greater than the given tip, both right LEDs illuminate for half a second. if y==x: for i in range(5): GPIO.output(LED1, 1) GPIO.output(LED2, 1) for j in range(100): za(x * 100 + t) GPIO.output(LED1, 0) GPIO.
24th day 24. Day Today in the Advent calendar • 1 button • Download code The connection diagram of the seven-segment display is the same as for the previous days. The decimal point is not used. Christmas carols on the Raspberry Pi The experiment of the 24th day plays a Christmas carol when a button is pressed. There are four different carols to choose from. You can download Christmas carols in mp3 format using the download code in the Advent calendar today.
t3=27 t4=17 GPIO.setup(t1, GPIO.setup(t2, GPIO.setup(t3, GPIO.setup(t4, GPIO.IN, GPIO.IN, GPIO.IN, GPIO.IN, GPIO.PUD_DOWN) GPIO.PUD_DOWN) GPIO.PUD_DOWN) GPIO.PUD_DOWN) number=[ "abcdef", #0 "bc", #1 "abdeg", #2 "abcdg", #3 "bcfg", #4 "acdfg", #5 "acdefg," #6 "abc", #7 "abcdefg", #8 "abcdfg", #9 ] print("Ctrl+C ends the program") def za(n): for s in "abcdefg": GPIO.output(seg[s], 0) for i in range(4): GPIO.output(digit[i], 1) if n!=0: for s in number[n]: GPIO.output(seg[s], 1) GPIO.
How it works In addition to the already known libraries, the library subprocess is imported. This makes it possible to start Linux command line programs from a Python program. def za(n): for s in "abcdefg": GPIO.output(seg[s], 0) for i in range(4): GPIO.output(digit[i], 1) if n!=0: for s in number[n]: GPIO.output(seg[s], 1) GPIO.output(digit[n-1], 0) The seven-segment display should display the number of the carol.
Vorsichtsmaßnahmen Auf keinen Fall irgendwelche GPIO-Pins miteinander verbinden und abwarten, was passiert. Nicht alle GPIO-Pins lassen sich frei programmieren. Einige sind für die Stromversorgung und andere Zwecke fest eingerichtet. Einige GPIO-Pins sind direkt mit Anschlüssen des Prozessors verbunden, ein Kurzschluss kann den Raspberry Pi komplett zerstören. Verbindet man über einen Schalter oder eine LED zwei Pins miteinander, muss immer ein Schutzwiderstand dazwischengeschaltet werden.