<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Bembyho blog</title>
    <description>My online CV, job references and professional experience with personal and research blog.
</description>
    <link>https://www.bambusekd.cz/</link>
    <atom:link href="https://www.bambusekd.cz/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Thu, 08 Apr 2021 17:46:42 +0200</pubDate>
    <lastBuildDate>Thu, 08 Apr 2021 17:46:42 +0200</lastBuildDate>
    <generator>Jekyll v3.8.5</generator>
    
      <item>
        <title>Vaječný likér</title>
        <description>&lt;p&gt;Vaječný likér jsem nikdy neměl moc v oblibě. Když se řekne vaječnák, vybaví se mi Velikonoce a návšteva tety Jiřiny. Ta nám za našich mladých let, když usoudila, že nám ještě nebylo dost na to, abychom dostali štamrplu slivovice (tedy něco kolem patnácti), vždycky nalila minimálně půldecák jakési žluté táhnoucí se hmoty. Jelikož nám ale většinou jinde tu štamplu stejně nalili, pobila se nám tato mňamina v břichu dohromady se slivovicou, což mělo za následek, že minimálně jeden z nás ogarů Velikonoční štaci zdárně nedokončil a místo tradičního oběda u babičky skončil někde v křáku, kde to musel vydýchat, vyležet, či se poléčit jiným způsobem.&lt;/p&gt;

&lt;p&gt;Nikdy mě tedy nelákalo vaječňák si někde dát, jelikož jsem to považoval za takové to hnusné tetičkovské pitíčko, až na mě někdě vzbafl příspěvek od Martina Žufánka o vaječnáku, ze kterého si cvrnknete do gatí. Ve svém příspěvku se pozastavoval nad tím, jak češi patlají vaječňáky z tuzemáku, salka a vanilinového cukru, tedy z věcí, které jsou lacinými napodobeninami originálů. Co dobrého asi může vzejít, když je člověk použije. No, asi něco jako vaječnák tety Jiřiny. Rozhodl jsem se tedy vyzkoušet jeho recept (ten vychází z receptu od Jany Florentýny Zatloukalové), založený na kvalitních surovinách a výsledek mě nadchl. Proto jsem se rozhodl recept na svém blogu zvěčnit, abych ho měl případně vždy po ruce.&lt;/p&gt;

&lt;h2 id=&quot;recept&quot;&gt;Recept&lt;/h2&gt;

&lt;p&gt;Budeme potřebovat:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;prázdnou 0,5 litrovou sklenici&lt;/li&gt;
  &lt;li&gt;4 žloutky&lt;/li&gt;
  &lt;li&gt;70 g moučkového cukru&lt;/li&gt;
  &lt;li&gt;1 lusk vanilky&lt;/li&gt;
  &lt;li&gt;250 ml 33% smetany&lt;/li&gt;
  &lt;li&gt;220 ml bourbonu&lt;/li&gt;
  &lt;li&gt;30 ml rumu&lt;/li&gt;
  &lt;li&gt;30 ml brandy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Výsledkem nám bude 0,5 litru vynikajícího vaječného likéru o obsahu alkoholu okolo 24%, který nás vyjde na asi 400 Kč.&lt;/p&gt;

&lt;h2 id=&quot;postup&quot;&gt;Postup&lt;/h2&gt;

&lt;p&gt;Jelikož jsou základem vaječnáku syrové žloutky, je důležité dodržet při jeho přípravě určitá hygienická opatření, aby nám po týdnech ležení v ledničce sám neodešel, případně abychom si po jeho požití nemuseli hrát na Usaina Bolt na trase obyvák záchod. Proto ze všeho nejdříve ve varné konvici přivedeme k varu 0,5 litru vody a ještě vroucí nalejeme do připravené sklenice a necháme 10 minut stát. Tímto účinně lahev sterilizujeme. Po deseti minutách můžete vodu vylít a nechat sklenici okapat. V průběhu výroby vás to určitě bude lákat ochutnat, což je v pořádku, ale vemte si an to pokaždé čistou lžičku. Ve vaši puse žije spoustu breberek, kterým by vaječnák taky moc chutnal a taková olízlá lžička je pro ně expres lístek.&lt;/p&gt;

&lt;p&gt;V misce nebo malém rendlíčku o objemu větším než 0,5 litru rozmícháme vidličkou žloutky, nejlépe od slepiček co mohly lítat po venku a dostalo se jim dobrého zobání, aby byly krásně žluté,cukr moučku a vnitřkem z vanilkového lusku. Lehce kvedláme, dokud nám nevznikne žlutá kašička s černýma ťupkama od vanilky. Do kašičky přidáme smetanu a opět lehce vidličkou mícháme, dokud se nám kašička se smetanou nespojí. Tímto máme hotový základ, který teď ochutíme alkoholem.&lt;/p&gt;

&lt;p&gt;Přidáme bourbon, doporuču Bourbon Bulleit Frontier. Má lahodnou chuť a lehce medový nádech a přitom je to cenově dostupný alkohol. Dále přidáme 30 ml rumu, tím myslím opravdového třtinového rumu, ne tuzmemáku. Výběr nechám na vás, pokud se nechcete pouštět do neznámých vod, můžete sáhnout po rumu Republika nebo Heffron. Je to takový dotupný základ, jinak zvolte podle chuti. Poslední přísada je 30 ml brandy. Brandy je vinná pálenka, která zrála několik let v dubovém sudu. Já jsem se ji rozhodl nahradit vyzrálou ovocnou pálenkou, konkrétně vlastní meruňkovicí, co rok ležela v dubovém soudku. Pokud nemáte vlastní vyzrálou pálenku (pokud pálíte, určitě si soudek na zrání pořiďte, vyplatí se to), můžete použít některou z pálenek z dubového sudu od Žufánka.&lt;/p&gt;

&lt;p&gt;Vše dobře vidličkou nebo metličkou promíchejte a nalejte do lahve. Výsledné množství by mělo vystačit na půl litru a ještě vám malá trocha zbyde na okamžité ochutnání. Lahev dejte na 3 týdny do lednice. Po třech týdnech se všechny chutě spojí, více vyniknou a likér získá krásnou žlutou barvu.&lt;/p&gt;

&lt;p&gt;Nyní je likér hotov a zbýva si ho jen vychutnat. Na zdraví!&lt;/p&gt;

</description>
        <pubDate>Sat, 12 Dec 2020 00:00:00 +0100</pubDate>
        <link>https://www.bambusekd.cz/blog/vajecny-liker</link>
        <guid isPermaLink="true">https://www.bambusekd.cz/blog/vajecny-liker</guid>
        
        <category>vanoce</category>
        
        <category>velikonoce</category>
        
        <category>alkohol</category>
        
        
        <category>recepty</category>
        
      </item>
    
      <item>
        <title>Measuring temperature with 1-Wire DS18B20 on Raspberry</title>
        <description>&lt;h2 id=&quot;1-wire&quot;&gt;1-Wire&lt;/h2&gt;

&lt;p&gt;In todays article we will look at an interesting 1-Wire technology and at how it works together with Raspberry. After the brief introduction, you can look forward to a practical demostration of how to use 1-Wire temperature sensor DS18B20.&lt;/p&gt;

&lt;p&gt;1-Wire is a device communications bus system developed by Dallas Smiconductors, simply put, it is a technology providing communication between multiple devices. 1-Wire always has one &lt;strong&gt;master&lt;/strong&gt; device, which runs the communications with the &lt;strong&gt;slave&lt;/strong&gt; devices. This technology is usually used for small and cheap sensors, such as thermometers and other similar devices, that only need low power and transmit only small amounts of data. 1-Wire can work on relatively large distances and needs only 3 wires. Those are &lt;strong&gt;power supply&lt;/strong&gt;, that can be anyting from 3V to 5V, &lt;strong&gt;ground&lt;/strong&gt; and &lt;strong&gt;data&lt;/strong&gt;. Slave devices are all conencted to the same triple of wires.&lt;/p&gt;

&lt;h2 id=&quot;raspberry-and-1-wire&quot;&gt;Raspberry and 1-Wire&lt;/h2&gt;

&lt;p&gt;If you want to work with 1-Wire devices on your Raspberry, you first need to enable the 1-Wire protocol. You can do this in Raspberry’s configuration and you have multiple options how to do it. You can change the configuration through the &lt;code class=&quot;highlighter-rouge&quot;&gt;raspi-config&lt;/code&gt; wizard. Simply open the console and write:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo raspi-config
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which will give you this screen:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-1-wire-temperature-ds18b20/config1.jpg&quot; alt=&quot;config_step_1&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Select section &lt;em&gt;5 Interfacing options&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-1-wire-temperature-ds18b20/config2.jpg&quot; alt=&quot;config_step_2&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Select &lt;em&gt;P7 1-Wire&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-1-wire-temperature-ds18b20/config3.jpg&quot; alt=&quot;config_step_3&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And select &lt;em&gt;Yes&lt;/em&gt; to enable 1-Wire&lt;/p&gt;

&lt;p&gt;The other option is more straightforward. You can edit the configuration file directly from any text editor, for example &lt;code class=&quot;highlighter-rouge&quot;&gt;nano&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo nano /boot/config.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Scroll to the bottom of the file and add a new line with:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dtoverlay=w1-gpio
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Save and close the file (press &lt;kbd&gt;Ctrl+X&lt;/kbd&gt;, &lt;kbd&gt;Y&lt;/kbd&gt; - as yes to changes and press &lt;kbd&gt;Enter&lt;/kbd&gt; to confirm).&lt;/p&gt;

&lt;p&gt;Now you are ready to go! Reboot you Raspberry:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo shutdown -r
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And you can start connecting 1-Wire devices.&lt;/p&gt;

&lt;h2 id=&quot;connecting-1-wire-devices-to-raspberry&quot;&gt;Connecting 1-Wire devices to Raspberry&lt;/h2&gt;

&lt;p&gt;Connecting 1-Wire devices to Raspberry is pretty easy, you only need 1 resistor with resistance between 4k7Ω and 10kΩ, that will be put between power supply and data line. That is it. For demonstration, we will use temperature sensors DS18B20. Get at least two, so we can see that 1-Wire is able to work with multiple devices, even in case they are of the same type.&lt;/p&gt;

&lt;h3 id=&quot;multiple-devices-on-one-line&quot;&gt;Multiple devices on one line&lt;/h3&gt;

&lt;p&gt;As was said before, 1-Wire always has one master device, which runs the communication - this will be our Raspberry and can have multiple slave devices - those will be temperature sensors in our case and they are all connected to just one data line. You might be asking how will the Raspberry know, which temperature sensor is sending which data, if they both sit on the same wire and the answer is simple. Each 1-Wire device has its own internal &lt;a href=&quot;https://en.wikipedia.org/wiki/Read-only_memory&quot;&gt;ROM memory&lt;/a&gt;, that stores unique 64-bit long address or identifier if you want. Every time a sensor sends some data, it also includes its address, so Raspberry always knows, which sensor sent which data.&lt;/p&gt;

&lt;h3 id=&quot;ds18b20-temperature-sensor&quot;&gt;DS18B20 temperature sensor&lt;/h3&gt;

&lt;p&gt;For this tutorial, we are using DS18B20 temperature sensor which is a great sensor with high accuarcy and can handle big range of temperatures. All the details can be found in its &lt;a href=&quot;https://gitlab.com/bambusekd-dev-blog/raspberry-1-wire-temperature-ds18b20/blob/e72ba4a11da3b5735fd201704d7d1e1e4c84692f/datasheet.pdf&quot;&gt;datasheet&lt;/a&gt;, but lets just point out the important details:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;power supply from 3V to 5V&lt;/li&gt;
  &lt;li&gt;measures temperatures from -55°C to +125°C (-67°F to +257°F)&lt;/li&gt;
  &lt;li&gt;accuarcy is ±0.5°C for temperatures ranging from -10°C to +85°C&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is also goot to know, which DS18B20 pins are for power supply, ground and data:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-1-wire-temperature-ds18b20/datasheet.jpg&quot; alt=&quot;DS18B20 datasheet&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can buy this sensor on Ebay, its price starts around 1.5 USD. This sensor comes also in a waterproof version, usually with a 2m cable, which makes it ideal for measuring temperatures in pools or refrigirators.&lt;/p&gt;

&lt;div class=&quot;post-images&quot;&gt;
    &lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-1-wire-temperature-ds18b20/ds18b20.jpg&quot; alt=&quot;DS18B20 sensor&quot; /&gt;
    &lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-1-wire-temperature-ds18b20/ds18b20-waterproof.jpg&quot; alt=&quot;DS18B20 waterproof sensor&quot; /&gt;
&lt;/div&gt;

&lt;h3 id=&quot;detecting-1-wire-devices&quot;&gt;Detecting 1-Wire devices&lt;/h3&gt;

&lt;p&gt;Lets get into wiring so we can finally see some real outputs. Grab your Raspberry, DS18B20 sensors and one 4k7Ω resistor. By default, the 1-Wire input pin on Rasperry is &lt;code class=&quot;highlighter-rouge&quot;&gt;GPIO 4&lt;/code&gt; in &lt;a href=&quot;http://bambusekd.cz/dev/raspberry-pinouts-bcm-board-wiringpi&quot;&gt;BCM&lt;/a&gt; numbering, but you can change that if you want. Go back to Raspberry’s config file:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo nano /boot/config.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And change the line with&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dtoverlay=w1-gpio
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dtoverlay=w1-gpio,gpiopin=X
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;where &lt;code class=&quot;highlighter-rouge&quot;&gt;X&lt;/code&gt; is the desired BCM pin number. Nertheless, for this example, we will work with the default settings.&lt;/p&gt;

&lt;p&gt;As was already mentioned before, wiring the sensors to the Raspberry is very simple, just connect the power and ground to corresponding sensor pins, connect &lt;code class=&quot;highlighter-rouge&quot;&gt;GPIO4&lt;/code&gt; with data pins and put the 4k7Ω resistor between the power and data line.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-1-wire-temperature-ds18b20/wiring_bb.svg&quot; alt=&quot;wiring&quot; class=&quot;post-image-100 post-image-500px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now we are ready to read the temperature data. All connected 1-Wire devices will create a folder named after its unique identifier in &lt;code class=&quot;highlighter-rouge&quot;&gt;/sys/bus/w1/devices/&lt;/code&gt;. To see the folders, use your command line:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ls /sys/bus/w1/devices/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In my case it gives me this output:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;28-000009f8259a  28-000009f96000  w1_bus_master1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lets ignore the &lt;code class=&quot;highlighter-rouge&quot;&gt;w1_bus_master1&lt;/code&gt;, the rest of the folders are our sensors. The first two digits tell us, what kind of a sensor we are dealing with, &lt;code class=&quot;highlighter-rouge&quot;&gt;28&lt;/code&gt; stands for DS18B20 temperature sensor, so everything is working as expected.&lt;/p&gt;

&lt;h4 id=&quot;detecting-all-1-wire-devices&quot;&gt;Detecting all 1-Wire devices&lt;/h4&gt;

&lt;p&gt;I have written a neat python script, that prints out data about all connected 1-Wire devices. You can find it on my &lt;a href=&quot;https://gitlab.com/bambusekd-dev-blog/raspberry-1-wire-temperature-ds18b20&quot;&gt;Gitlab repository for this tutorial&lt;/a&gt;. Grab it directly from command line:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone git@gitlab.com:bambusekd-dev-blog/raspberry-1-wire-temperature-ds18b20.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are two files in this repository, for detecting devices, use the &lt;code class=&quot;highlighter-rouge&quot;&gt;w1-devices.py&lt;/code&gt;. So in your command line:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd raspberry-1-wire-temperature-ds18b20
python w1-devices.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The output for me is:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; _______________________
|
| Device #1
| Type:        temperature sensor
| ID:          000009f96000
| Sensor type: DS18B20
|_______________________

 _______________________
|
| Device #2
| Type:        temperature sensor
| ID:          000009f8259a
| Sensor type: DS18B20
|_______________________
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;reading-data-from-ds18b20&quot;&gt;Reading data from DS18B20&lt;/h3&gt;

&lt;p&gt;To read the actual measured sensor data, we need to read the contents of a file called &lt;code class=&quot;highlighter-rouge&quot;&gt;w1_slave&lt;/code&gt;, that is included in each device file in &lt;code class=&quot;highlighter-rouge&quot;&gt;/sys/bus/w1/devices/&lt;/code&gt;. To do that, use your command line and don’t forget to change the folder name to the name of your sensors:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat /sys/bus/w1/devices/28-000009f8259a/w1_slave
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I get this output:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;92 01 4b 46 7f ff 0e 10 24 : crc=24 YES
92 01 4b 46 7f ff 0e 10 24 t=25125
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;WTF is this might be your first reaction, but everything we need to know is written in this output. Lets have a look at the end of the first line. Here we can see if the information was received error-free. If the word says &lt;code class=&quot;highlighter-rouge&quot;&gt;YES&lt;/code&gt;, we are fine, if it states &lt;code class=&quot;highlighter-rouge&quot;&gt;NO&lt;/code&gt;, there was some problem. At the end of the second line, we have the measured temperature, it just needs to be divided by 1000. So &lt;code class=&quot;highlighter-rouge&quot;&gt;t=25125&lt;/code&gt;, gives us 25125/1000 = 25.125°C. Pretty hot day for May in Brno.&lt;/p&gt;

&lt;p&gt;Usually, you want to read the measured data automatically running a script, so doing it in a command line is not very practical. So lets write a Python script, that will do it for us.&lt;/p&gt;

&lt;h2 id=&quot;reading-ds18b20-sensor-data-in-python&quot;&gt;Reading DS18B20 sensor data in Python&lt;/h2&gt;

&lt;p&gt;If you have already downloaded my &lt;a href=&quot;https://gitlab.com/bambusekd-dev-blog/raspberry-1-wire-temperature-ds18b20&quot;&gt;Gitlab repository for this tutorial&lt;/a&gt;, you can find the script there under &lt;code class=&quot;highlighter-rouge&quot;&gt;w1-read-temperature-ds18b20.py&lt;/code&gt;. It detects all DS18B20 sensors and prints out the mesured temperature. Running the script&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python w1-read-temperature-ds18b20.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;will output something like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; _________________________
|
| Measurement from: 2018-05-07 18:42:41
|________
|
| Thermometer 28-000009f96000
| Celsius:    25.25
| Fahrenheit: 77.45
|________
|
| Thermometer 28-000009f8259a
| Celsius:    25.062
| Fahrenheit: 77.1116

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For completness, I am including the script also directly into this blog post below. Read the in code comments to understand the functionality.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/python
&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;re&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;glob&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glob&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Folder with 1-Wire devices
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w1DeviceFolder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'/sys/bus/w1/devices'&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Function that returns array with IDs of all found thermometers
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;find_thermometers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Get all devices
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;w1Devices&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w1DeviceFolder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'/*/'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Create regular expression to filter only those starting with '28', which is thermometer
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;w1ThermometerCode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;r'28-\d+'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Initialize the array
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;thermometers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Go through all devices
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;device&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w1Devices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Read the device code
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;deviceCode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w1DeviceFolder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# If the code matches thermometer code add it to the array
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w1ThermometerCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deviceCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;thermometers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deviceCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Return the array
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;thermometers&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Function that reads and returns the raw content of 'w1_slave' file
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_temp_raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deviceCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w1DeviceFolder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'/'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deviceCode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'/w1_slave'&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'r'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readlines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Function that reads the temperature from raw file content
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deviceCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Read the raw temperature data
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;read_temp_raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deviceCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Wait until the data is valid - end of the first line reads 'YES'
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'YES'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;read_temp_raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deviceCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Read the temperature, that is on the second line
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;equals_pos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'t='&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equals_pos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;temp_string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;equals_pos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Convert the temperature number to Celsius
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;temp_c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1000.0&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Convert the temperature to Fahrenheit
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;temp_f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp_c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;9.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;32.0&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Return formatted sensor data
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'thermometerID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deviceCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'celsius'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp_c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'fehrenheit'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp_f&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Function that pretty prints the sensor data
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'|________'&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'|'&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'| Thermometer {}'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'thermometerID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'| Celsius:    {}'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'celsius'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'| Fahrenheit: {}'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'fehrenheit'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Function that prints actual timestamp
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;' _________________________'&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'|'&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'| Measurement from: '&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strftime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Y-&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;m-&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;d &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;H:&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;M:&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;S'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Main function
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Find all connected thermometers
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;thermometers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;find_thermometers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Print actual timestamp
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;print_timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Go through all connected thermometers
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;thermometer&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;thermometers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Pretty print sensor data
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;print_temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;thermometer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    
&lt;span class=&quot;c1&quot;&gt;# Run the main function when the script is executed
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If everything worked, then congratz! You have successfuly configured, wired and read 1-Wire sensor data!&lt;/p&gt;
</description>
        <pubDate>Mon, 07 May 2018 00:00:00 +0200</pubDate>
        <link>https://www.bambusekd.cz/dev/raspberry-1-wire-temperature-ds18b20</link>
        <guid isPermaLink="true">https://www.bambusekd.cz/dev/raspberry-1-wire-temperature-ds18b20</guid>
        
        <category>raspberry</category>
        
        <category>1-Wire</category>
        
        <category>temperature</category>
        
        <category>DS18B20</category>
        
        
        <category>raspberry</category>
        
      </item>
    
      <item>
        <title>Psí výběh Brno-Vinohrady</title>
        <description>&lt;h2 id=&quot;participativní-rozpočet-brno&quot;&gt;Participativní rozpočet Brno&lt;/h2&gt;

&lt;p&gt;Brno v roce 2017 spustilo projekt &lt;a href=&quot;https://damenavas.brno.cz/&quot;&gt;Dáme na Vás&lt;/a&gt;, ve kterém sbírá nápady od svých občanů na vylepšení našeho města. Pokud má někdo námět na něco, co by se mohlo v Brně postavit, uspořádat, vylepšit nebo revitalizovat, má možnost svůj nápad jednoduchou cestou přihlásit do projektu - stačí sepsat stručný popis projektu, udělat nástřel rozpočtu a je to. Aby se město začalo projektem zabývat, potřebuje člověk 300 lajků nebo 30 podpisů od Brňáků. Po získání podpory projekt projde posouzením, zda je realizovatelný a pokud ano, postupuje do závěrečného hlasování. V tom se rozhodne, opět podle hlasů Brňáků, které projekty se budou realizovat.&lt;/p&gt;

&lt;p&gt;Po prvním ročníku, ve kterém se nakonec vybralo 16 projektů, které by se měly v letošním roce (2018) začít realizovat, byl v únoru spuštěn ročník druhý. Vzhledem k tomu, že bydlím na brněnských Vinohradech a přijde mi, že je tady spoustu věcí ke zlepšení, rozhodl jsem se přispět svou troškou do mlýna a vypracovat návrh na psí výběh, který tady na Vinohradech chybí. V okolních městských částech se už několik výběhů vybudovalo a myslím si, že svou funkci plní perfektně. Lidé mohou své pejsky vypustit v oploceném výběhu, beze strachu, že jim někam utečou, což má za důsledek méně na volno puštěných pejsků na sídlišti samotném. Pejskaři mají své místo, kam s pejsky zajít a ti, kteří pejsky nemusí, mohou být o to víc v klidu, jelikož místo v ulicích pejsci lítají a blbnou právě ve výběhu.&lt;/p&gt;

&lt;h2 id=&quot;psí-výběh-na-vinohradech&quot;&gt;Psí výběh na Vinohradech&lt;/h2&gt;

&lt;p&gt;Vinohradům, jakožto hustě zastavenému sídlišti dle mého psí výběh citelně chybí, jelikož zde není žádné větší prostranství, kde by bylo možné psy venčit, aniž by měl člověk obavy s možnou kolizí s automobily a byl alespoň trochu mimo panelákovou zástavbu a cítil se jako v přírodě. V okolních městských částech v posledních letech pěkné psí výběhy vznikly a všichni si je chválí. Spoustu místních pejskařů si tak přeje, aby psí výběh existoval i zde na Vinohradech.&lt;/p&gt;

&lt;p&gt;Abych byl přesný, na Vinohradech psí výběh existuje. Nachází se za bývalou ZŠ na Bzenecké ulici, ale je to jen kus louky, bez jakéhokoliv oplocení, pouze s jedním košem na odpadky, na kterém je cedule “Výběh pro psy” a pro mě prapodivnými dřevěnými konstrukcemi, nad jejichž významem stále bádám - snad opěrné “lavičky”, překážky pro medvědy… netuším. Zkuste si tipnout sami, viz foto níže.&lt;/p&gt;

&lt;iframe src=&quot;https://api.mapy.cz/frame?params=%7B%22x%22%3A16.653993474554795%2C%22y%22%3A49.20760677090886%2C%22base%22%3A%221%22%2C%22layers%22%3A%5B%5D%2C%22zoom%22%3A17%2C%22url%22%3A%22https%3A%2F%2Fmapy.cz%2Fs%2F2rfyL%22%2C%22mark%22%3A%7B%22x%22%3A%2216.6552862993%22%2C%22y%22%3A%2249.2078713737%22%2C%22title%22%3A%22Ps%C3%AD%20v%C3%BDb%C4%9Bh%20%C5%BDidenice%22%7D%2C%22overview%22%3Afalse%7D&amp;amp;width=700&amp;amp;height=466&amp;amp;lang=cs&quot; padding=&quot;10px&quot; width=&quot;100%&quot; height=&quot;250px&quot; max-height=&quot;75vh&quot; style=&quot;border:none&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/blog/psi-vybeh-vinohrady/vybeh_stavajici.jpg&quot; alt=&quot;vybeh_stavajici&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Pro tento výběh nevýběh jsem se pokusil navrhnout novou podobu. Původně jsem chtěl pro návrh využít veškeré volné prostranství na stávajícím místě, kde by výběh mohl mít plochu zhruba 1500 m2.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/blog/psi-vybeh-vinohrady/vybeh_puvodni_mapa.jpg&quot; alt=&quot;vybeh_puvodni_navrh&quot; class=&quot;post-image-100&quot; /&gt;
&lt;img src=&quot;https://www.bambusekd.cz/images/blog/psi-vybeh-vinohrady/vybeh_puvodni.jpg&quot; alt=&quot;vybeh_puvodni_navrh&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Bohužel jsem zjistil, že ne všechny pozemky na tomto místě jsou města Brna a tak bylo nutné projekt předělat na menší, zhruba 879 m2 velký výběh. Ten je již čistě na pozemcích města.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/blog/psi-vybeh-vinohrady/mapa_rozmery.jpg&quot; alt=&quot;vybeh_mapa_rozmery&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Výběh bude oplocený (asi 126 metrů plotu) s jednou brankou s přístupem od cesty. Na cestě by se nově vybudoval bezbariérový přechod, aby se do výběhu dostali i lidé s omezenou možností pohybu.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/blog/psi-vybeh-vinohrady/vybeh.jpg&quot; alt=&quot;vybeh_navrh&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Přístupová cesta by byla vydlážděna zámkovou dlažbou, stejně tak jako malý ostrůvek uvnitř výběhu (36 m2), podél jehož okraje by byly dvě lavičky a mezi nimi strom. Tento ostrůvek bude zajišťovat, že se do výběhu dostanou lidé “suchou nohou” i za horšího počasí a budou mít odkud pozorovat své mazlíčky. Osvětlení budou zajišťovaly dvě lampy. Na okraji ostrůvku je umístěno pítko, pro zahnání žízně jak lidí, tak psů. Uvažoval jsem i o nádobě na vodu pro psy, ale byl jsem upozorněn, že by to z hlediska hygieny nebylo dobré, aby všichni psi pili z jedné společné nádoby, jelikož by se tak mohly velmi úspěšně mezi nimi šířit nemoci. U vchodu do výběhu jsou umístěny koše zvlášť na odpadky a zvlášť na psí exkrementy.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/blog/psi-vybeh-vinohrady/vybeh_posezeni.jpg&quot; alt=&quot;vybeh_posezeni&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Samotný výběh je volně rozdělen na dvě části. První z nich je lesík s asi 9 stromy, ve kterém v létě najdou lidé i psi stínek, druhou je pak část s překážkami pro psy a prostorem například pro hru s míčkem/diskem.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/blog/psi-vybeh-vinohrady/vybeh_park.jpg&quot; alt=&quot;vybeh_park&quot; class=&quot;post-image-100&quot; /&gt;
&lt;img src=&quot;https://www.bambusekd.cz/images/blog/psi-vybeh-vinohrady/vybeh_agility.jpg&quot; alt=&quot;vybeh_agility&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;rozpočet&quot;&gt;Rozpočet&lt;/h2&gt;

&lt;p&gt;I když takový výběh může vypadat jako levná záležitost, při práci na rozpočtu a hledání cen člověk zjistí, že se jednotlivé položky rychle nastřádají. Hrubý návrh rozpočtu pro tento projekt mi vyšel na nějakých 414 400 Kč, tedy skoro půl milionu. Počítám ale, že je to spíše dolní odhad možné ceny. Jednotlivé položky jsou rozepsány v následující tabulce.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;&lt;strong&gt;položka&lt;/strong&gt;&lt;/th&gt;
      &lt;th&gt;&lt;strong&gt;cena za kus&lt;/strong&gt;&lt;/th&gt;
      &lt;th&gt;&lt;strong&gt;množství&lt;/strong&gt;&lt;/th&gt;
      &lt;th&gt;&lt;strong&gt;cena celkem&lt;/strong&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;lavička&lt;/td&gt;
      &lt;td&gt;4000&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;8000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;koš na odpadky&lt;/td&gt;
      &lt;td&gt;3000&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;3000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;koš na exkrementy&lt;/td&gt;
      &lt;td&gt;3000&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;lampa&lt;/td&gt;
      &lt;td&gt;10000&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;20000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;dlažba&lt;/td&gt;
      &lt;td&gt;700&lt;/td&gt;
      &lt;td&gt;36 m2&lt;/td&gt;
      &lt;td&gt;25200&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;překážky pro psy&lt;/td&gt;
      &lt;td&gt;7000&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;35000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;stromy&lt;/td&gt;
      &lt;td&gt;3000&lt;/td&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;30000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;plot a branka&lt;/td&gt;
      &lt;td&gt;200&lt;/td&gt;
      &lt;td&gt;126 m2&lt;/td&gt;
      &lt;td&gt;25200&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;pítko&lt;/td&gt;
      &lt;td&gt;15000&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;15000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;chodník a přechod&lt;/td&gt;
      &lt;td&gt;100000&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;100000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;projekt&lt;/td&gt;
      &lt;td&gt;50000&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;50000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;práce&lt;/td&gt;
      &lt;td&gt;100000&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;100000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;celkem&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;414400&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;projekt&quot;&gt;Projekt&lt;/h2&gt;

&lt;p&gt;Návrh projektu jsem vypracoval ve &lt;a href=&quot;https://www.sketchup.com/&quot;&gt;SketchUp&lt;/a&gt;. Nejsem žádný architekt ani modelář, takže prosím o shovívavost. Projekt je možné nalézt přímo ve &lt;a href=&quot;https://3dwarehouse.sketchup.com/model/31fb8407-c191-4e06-84a6-858b86ad3614/Ps%C3%AD-v%C3%BDb%C4%9Bh-Brno-Vinohrady&quot;&gt;skladu modelů&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Po kliknutí na obrázek pod tímto odstavcem, by se měl spustit 3D model, ve kterém si návrh můžete prohlédnout ze všech stran.&lt;/p&gt;

&lt;iframe src=&quot;https://3dwarehouse.sketchup.com/embed.html?mid=31fb8407-c191-4e06-84a6-858b86ad3614&amp;amp;width=580&amp;amp;height=326&quot; frameborder=&quot;0&quot; scrolling=&quot;no&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; width=&quot;100%&quot; height=&quot;250px&quot; max-height=&quot;75vh&quot; padding=&quot;10px&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;Zde je pohled na celý výběh shora.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/blog/psi-vybeh-vinohrady/vybeh_pudorys.jpg&quot; alt=&quot;vybeh_shora&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Model jsem se pokusil zasadit i přímo do pohledu na stávající výběh.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/blog/psi-vybeh-vinohrady/vybeh_matched.jpg&quot; alt=&quot;vybeh_foto&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Pokud se vám můj návrh na projekt líbí, budu rád za jakoukoliv podporu - projekt najdete oficiálně &lt;a href=&quot;https://damenavas.brno.cz/projekt/?id=759&quot;&gt;zde&lt;/a&gt;. Pro zájemce je možné všechny zdrojové i výsledné vyexportované soubory najít na mém &lt;a href=&quot;https://gitlab.com/bambusek/psi-vybeh-vinohrady&quot;&gt;Gitlab repozitáři&lt;/a&gt;. Pokud máte nějaké dotazy, nápady na vylepšení nebo kritiku, budu rád pokud se ozvete pod článkem v komentářích nebo mi napíšete mail na &lt;a href=&quot;mailto:bambusekd@gmail.com&quot;&gt;můj email&lt;/a&gt;. Díky!&lt;/p&gt;

</description>
        <pubDate>Sat, 03 Mar 2018 00:00:00 +0100</pubDate>
        <link>https://www.bambusekd.cz/blog/psi-vybeh-vinohrady</link>
        <guid isPermaLink="true">https://www.bambusekd.cz/blog/psi-vybeh-vinohrady</guid>
        
        <category>brno</category>
        
        <category>psí-výběh</category>
        
        <category>participativní-rozpočet</category>
        
        <category>damenavas</category>
        
        
        <category>brno</category>
        
      </item>
    
      <item>
        <title>Parking sensor made with HC-SR04 and Raspberry</title>
        <description>&lt;h2 id=&quot;parking-sensor&quot;&gt;Parking sensor&lt;/h2&gt;

&lt;p&gt;In today’s article, we are going to build a parking sensor. Most of you are for sure familiar with that beeping sound in your car that starts every time you start to back your car and tells you, by changing the beeping frequency, how far your rear spoiler is from the closest obstacle. If you are not, I have just described to you what a parking sensor is. We will build a simple circuit with Raspberry Pi and HC-SR04 ultrasonic range sensor, that will simply beep according to how far it is from an obstacle. No beeping will tell us that everything around us is far away. Slow beeping will start as we will be getting closer and the closer we will be the higher will the beeping frequency be. At last, our sensor wil will beep constantly as we reach dangerously close position to our obstacle.&lt;/p&gt;

&lt;h2 id=&quot;distance-sensor-hc-sr04&quot;&gt;Distance sensor HC-SR04&lt;/h2&gt;

&lt;p&gt;HC-SR04 is an ultrasonic range sensor. I have already written an &lt;a href=&quot;http://bambusekd.cz/dev/raspberry-measure-distance-JSN-SR04T&quot;&gt;article about JSN-SR04T sensor&lt;/a&gt;, which is also an ultrasonic range sensor, so if you want to know how exactly such a sensor works, go ahead and read the other article, and then come back here for the rest of the tutorial. But in a nutshell - an ultrasonic sensor has two membranes (those two tubes on HC-SR04). One sends a sound wave (on a high frequency, so we do not hear it), which travels to an obstacle, where it is reflected back to the sensor, where the second membrane registers it as it comes back and immediately sets one of its output pins to HIGH value. To measure the distance, we just take the time between sending the wave and receiving it, divide it by two, because it had to travel there and back and since we know how fast sound travels, we can compute how far the obstacle is.&lt;/p&gt;

&lt;p&gt;HC-SR04 can measure distances from 2cm to 4 meters in angle up to 15 degrees and needs at least 5V to operate. When doing consecutive measurements, we should always wait at least 60ms between two of them, in order to reduce interference that could be caused by too many signals travelling there and back.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-parking-sensor-hc-sr04/sensor.png&quot; alt=&quot;HC-SR04&quot; class=&quot;post-image-50-alone&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;connecting-it-to-raspberry&quot;&gt;Connecting it to Raspberry&lt;/h2&gt;

&lt;p&gt;HC-SR04 has 4 pins, VCC which needs to be connected to 5V input, GND that has to be connected to ground and then two control pins TRIGGER and ECHO. In order to make the sensor send a sound wave, we need to send at least 10µs long HIGH pulse to TRIGGER pin. That means we will connect the pin with a GPIO, set it to OUTPUT mode and instruct Raspberry to set the GPIO to LOW, then very shortly to HIGH and back to LOW. At the point, when TRIGGER will switch from HIGH to LOW, the sensor will send a sound pulse. The second pin - ECHO is always in LOW, except for the moment, when it registers incoming sound wave, then it will shortly rise to HIGH. ECHO therefore needs to be connected to a GPIO set to INPUT mode, where we will be waiting for it to jump to HIGH. So far it seems like a straightforward and easy setup, but there is one catch. We can not connect ECHO pin directly to GPIO. ECHO in HIGH state gives 5V which is too high for Raspberry and could damage it. Raspberry operates with 3,3V as HIGH value, so we need to reduce the voltage coming from ECHO.&lt;/p&gt;

&lt;h3 id=&quot;voltage-divider&quot;&gt;Voltage Divider&lt;/h3&gt;

&lt;p&gt;But worry not, reducing voltage from higher value to a lower one is very easy and we will only need two resistors. Lets take 330Ω and 470Ω. Put 330Ω resistor between ECHO output and GPIO, then 470Ω resistor between ground and the point where 330Ω resistor meets the GPIO connection. See the the complete circuit schema below for better understanding.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-parking-sensor-hc-sr04/schematics.png&quot; alt=&quot;Schema&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The thing we just build with resistors is called a &lt;em&gt;voltage divider&lt;/em&gt;. 5V coming from the source will be divided on resistors, serially connected, according to their resistance ratio, which is in our case 330:470. To better understand how exactly voltage divider works, lets ignore the rest of the Raspberry circuit and just see the resistors as is illustrated on the image below.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-parking-sensor-hc-sr04/voltage_divider.png&quot; alt=&quot;Voltage divider&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Vin&lt;/strong&gt; is our 5V source. &lt;strong&gt;Vout&lt;/strong&gt; is the place between resistors, from where we will be taking the lowered voltage for GPIO input. Ground is obviously the last end of circuit marked with shrinking lines. Resistors are marked as &lt;strong&gt;Z1&lt;/strong&gt; and &lt;strong&gt;Z2&lt;/strong&gt;. If you remember &lt;strong&gt;Ohm’s law&lt;/strong&gt;, which is the cornerstone of electronics, it says that voltage is equal to resistance multiplied by current.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;U = R * I
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We also know, that if all the parts of circuit are in a series, which is our case, the current flowing through them will be the same. And last important thing - in serial circuit the total resistance is the sum of resistance of all resistors. Taking all of this into account, we can count the current flowing through the circuit.&lt;/p&gt;

&lt;p&gt;From&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;U = R * I
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;we see that&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;I = U/R
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;which is&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;I = U/(Z1+Z2)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Putting the known numbers to equation we get that&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;I = 5/800 = 0.00625A = 6.25mA
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;OK, we have the current value, so now we can count what voltage is on each of the two resistor. Keep in mind that voltage is always measured between two points in circuit. Measuring voltage on &lt;strong&gt;Z1&lt;/strong&gt; equals to measuring voltage between &lt;strong&gt;Vin&lt;/strong&gt; and &lt;strong&gt;Vout&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Take again Ohm’s law&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;U = R * I
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;now we know&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;I = 0.00625A
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;R = Z1 = 330Ω
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;so&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Uz1 = 0.00625 * 330 = 2.0625V
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And lets do the same with the second resistor, measuring voltage between points &lt;strong&gt;Vout&lt;/strong&gt; and ground will give us&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Uz2 = 0.00625 * 470 = 2.9375V
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can check that our calculations are correct by adding those two voltage values together. They will give us 5V so we see that no voltage disappeared and really divided according to value of resistors. All right, so the voltage got divided, now only the last step. Which of those values will be the voltage on &lt;strong&gt;Vout&lt;/strong&gt;? Well, voltage is always measured against ground, so the correct answer is 2.9V - the voltage between &lt;strong&gt;Vout&lt;/strong&gt; and ground which is equal to our &lt;strong&gt;Uz2&lt;/strong&gt;. This might be a bit counter intuitive if you think of a current flowing from the source, where we have 5V, down to the ground, where it might be tempting to see it as it will partially ‘leak’ on &lt;strong&gt;Vout&lt;/strong&gt;, so you would think the voltage between &lt;strong&gt;Vin&lt;/strong&gt; and &lt;strong&gt;Vout&lt;/strong&gt; is the correct answer, but no. Always remember that voltage is a value that is measured between two points, where one is ground. If you don§t believe me, just build the circuit and measure it…&lt;/p&gt;

&lt;p&gt;Also, you might be thinking - you said we need 3.3V, but we have 2.9V, why such a weird number. Well, we could easily deduct, that to get 3.3V from 5V we would need a resistors with 1:2 ratio, so lets say 1000Ω and 2000Ω, but the most common resistors usually don§’ have this exact value, so we are using the closet we have in this case 330Ω and 470Ω, which gives us 330:470 = 1:1.42 ratio, which is close enough.&lt;/p&gt;

&lt;p&gt;Raspberry’s input GPIO doesn’t necessarily need 3.3V to turn to HIGH state, in fact Raspberry will take anything between 1.3V and 3.3V as HIGH and anything between 0V and 0.8V as LOW, which is great, because we don’t have to be that precise when building our circuits.&lt;/p&gt;

&lt;h3 id=&quot;buzzer&quot;&gt;Buzzer&lt;/h3&gt;

&lt;p&gt;Last part of our circuit will be a buzzer. It is a simple electronic component, that simply beeps if it is connected to the source and it is silent when it is disconnected. We will use it to acknowledge the “driver” of an approaching obstacle. Buzzer comes it many variants, be sure to buy one that is made for 3.3V voltage that our Raspberry GPIO gives us when it is in HIGH state. Connect the buzzer to a GPIO set to OUTPUT mode and to the ground.&lt;/p&gt;

&lt;h2 id=&quot;coding-the-sensor&quot;&gt;Coding the sensor&lt;/h2&gt;

&lt;p&gt;Now lets create a file called parking_sensor.py and write a function &lt;code class=&quot;highlighter-rouge&quot;&gt;distance&lt;/code&gt;, that will measure how far the obstacle is from the sensor. It will be similar to the code from article about JSN-SR04T:&lt;/p&gt;

&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/python
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Import required Python libraries
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;RPi.GPIO&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# We will be using the BCM GPIO numbering
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setmode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BCM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Select which GPIOs you will use
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Set BUZZER to OUTPUT mode
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Set TRIGGER to OUTPUT mode
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Set ECHO to INPUT mode
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Measures the distance between a sensor and an obstacle and returns the measured value
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Send 10 microsecond pulse to TRIGGER
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# set TRIGGER to HIGH
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.00001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# wait 10 microseconds
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# set TRIGGER back to LOW
&lt;/span&gt; 
  &lt;span class=&quot;c1&quot;&gt;# Create variable start and assign it current time
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Create variable stop and assign it current time
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Refresh start value until the ECHO goes HIGH = until the wave is send
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
 
  &lt;span class=&quot;c1&quot;&gt;# Assign the actual time to stop variable until the ECHO goes back from HIGH to LOW = the wave came back
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
 
  &lt;span class=&quot;c1&quot;&gt;# Calculate the time it took the wave to travel there and back
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;measuredTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Calculate the travel distance by multiplying the measured time by speed of sound
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;distanceBothWays&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;measuredTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;33112&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# cm/s in 20 degrees Celsius
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Divide the distance by 2 to get the actual distance from sensor to obstacle
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;distanceBothWays&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Print the distance to see if everything works correctly
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Distance : {0:5.1f}cm&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Return the actual measured distance
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Main function
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Pause between measurements
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# If the program is ended, stop beeping and cleanup GPIOs
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;KeyboardInterrupt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Run the main function when the script is executed
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you run the script from your Raspberry&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python parking_sensor.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;it will print the distance each second. That is nice, but lets add the beeping. We will write another function, that will count how fast the buzzer should beep = beeping frequency. If the obstacle is more than 50cm from the sensor, it will not beep, that will be marked by value &lt;code class=&quot;highlighter-rouge&quot;&gt;-1&lt;/code&gt;. If the obstacle will be closer than 50cm, but further than 30cm, we will beep once per second - value returned will be &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt;. If the distance will be between 30cm and 20cm, we will beep twice a second - value returned will be &lt;code class=&quot;highlighter-rouge&quot;&gt;0.5&lt;/code&gt;. For distances between 20cm and 10 cm, we will beep four times a second - value returned will be &lt;code class=&quot;highlighter-rouge&quot;&gt;0.25&lt;/code&gt;. If we will be in a dangerously close position, that is closer than 10cm to obstacle, buzzer will beep constantly - value returned will be &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt;. Lets put that into code.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Calculates the frequency of beeping depending on the measured distance
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;beep_freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Measure the distance
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the distance is bigger than 50cm, we will not beep at all
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the distance is between 50 and 30 cm, we will beep once a second
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the distance is between 30 and 20 cm, we will beep every twice a second
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the distance is between 20 and 10 cm, we will beep four times a second
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the distance is smaller than 10 cm, we will beep constantly
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The only thing left is to adjust the main function to make the buzzer beep in our given frequency. We will do that by writing a simple if-else statement with result from the &lt;code class=&quot;highlighter-rouge&quot;&gt;beep_freq&lt;/code&gt; function.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c1&quot;&gt;# Main function
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Repeat till the program is ended by the user
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Get the beeping frequency
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;beep_freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# No beeping
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Constant beeping
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Beeping on certain frequency
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Beep is 0.2 seconds long
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Pause between beeps = beeping frequency
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# If the program is ended, stop beeping and cleanup GPIOs
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;KeyboardInterrupt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And that is it! The complete code is right below this paragraph and you can also get it from my &lt;a href=&quot;https://gitlab.com/bambusekd-dev-blog/raspberry-parking-sensor-hc-sr04&quot;&gt;Gitlab&lt;/a&gt;. Well done, you have just successfully built your own parking sensor. Enjoy experimenting with it!&lt;/p&gt;

&lt;h2 id=&quot;complete-program&quot;&gt;Complete program&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/python
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Import required Python libraries
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;RPi.GPIO&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# We will be using the BCM GPIO numbering
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setmode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BCM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Select which GPIOs you will use
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Set BUZZER to OUTPUT mode
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Set TRIGGER to OUTPUT mode
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Set ECHO to INPUT mode
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Measures the distance between a sensor and an obstacle and returns the measured value
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Send 10 microsecond pulse to TRIGGER
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# set TRIGGER to HIGH
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.00001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# wait 10 microseconds
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# set TRIGGER back to LOW
&lt;/span&gt; 
  &lt;span class=&quot;c1&quot;&gt;# Create variable start and assign it current time
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Create variable stop and assign it current time
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Refresh start value until the ECHO goes HIGH = until the wave is send
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
 
  &lt;span class=&quot;c1&quot;&gt;# Assign the actual time to stop variable until the ECHO goes back from HIGH to LOW = the wave came back
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
 
  &lt;span class=&quot;c1&quot;&gt;# Calculate the time it took the wave to travel there and back
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;measuredTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Calculate the travel distance by multiplying the measured time by speed of sound
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;distanceBothWays&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;measuredTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;33112&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# cm/s in 20 degrees Celsius
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Divide the distance by 2 to get the actual distance from sensor to obstacle
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;distanceBothWays&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Print the distance to see if everything works correctly
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Distance : {0:5.1f}cm&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Return the actual measured distance
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Calculates the frequency of beeping depending on the measured distance
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;beep_freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Measure the distance
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the distance is bigger than 50cm, we will not beep at all
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the distance is between 50 and 30 cm, we will beep once a second
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the distance is between 30 and 20 cm, we will beep every twice a second
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the distance is between 20 and 10 cm, we will beep four times a second
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the distance is smaller than 10 cm, we will beep constantly
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Main function
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Repeat till the program is ended by the user
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Get the beeping frequency
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;beep_freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# No beeping
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Constant beeping
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Beeping on certain frequency
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Beep is 0.2 seconds long
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Pause between beeps = beeping frequency
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# If the program is ended, stop beeping and cleanup GPIOs
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;KeyboardInterrupt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_BUZZER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Run the main function when the script is executed
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Wed, 24 Jan 2018 00:00:00 +0100</pubDate>
        <link>https://www.bambusekd.cz/dev/raspberry-parking-sensor-hc-sr04</link>
        <guid isPermaLink="true">https://www.bambusekd.cz/dev/raspberry-parking-sensor-hc-sr04</guid>
        
        <category>raspberry</category>
        
        <category>distance</category>
        
        <category>parking-sensor</category>
        
        <category>HC-SR04</category>
        
        <category>python</category>
        
        <category>electronics</category>
        
        <category>voltage-divider</category>
        
        
        <category>raspberry</category>
        
      </item>
    
      <item>
        <title>Measuring distance with JSN-SR04T and Raspberry</title>
        <description>&lt;h2 id=&quot;distance-sensor-jsn-sr04t&quot;&gt;Distance sensor JSN-SR04T&lt;/h2&gt;

&lt;p&gt;If you are in need of measuring distances with your Raspberry, this tutorial is what you where looking for. In this article, we will look at a JSN-SR04T ultrasonic distance sensor. Which is an affordable sensor for your robotics/home automation/whatever project. Its price starts on $7 on Ebay. For this price you will get a waterproof sensor, that can measure distances between 20cm to 600cm and should work in temperatures from -20°C to +70°C. It claims to have an accuracy of ±1cm, but lets be honest, there are so many things that can influence the measuring, that you will get a higher fluctuance in your measurements than that. This is how it looks like…&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-measure-distance-JSN-SR04T/sensor.jpg&quot; alt=&quot;JSN-SR04T&quot; class=&quot;post-image-50-alone&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;how-to-measure-distance-with-sound&quot;&gt;How to measure distance with sound&lt;/h2&gt;

&lt;p&gt;JSN-SR04T and ultrasonic distance sensor in general works on a simple principle. It sends an acoustic wave (ultrasonic, so you won’t hear it) when it is triggered and then listens for when the wave reflects from some obstacle in front of it and comes back. However, the sensor is only able to send the wave and tell that a wave came back, you will have to do the math to count the distance by yourself (read, you will have to program it by yourself). But read further and you will see it is really easy.&lt;/p&gt;

&lt;p&gt;From the sensor specification we can find out that the sensor is activated by sending a pulse of HIGH, which is at least 10µs (microseconds) long, on the TRIGGER pinout. Which means that if you want to send a wave, you will need to have a GPIO set to OUTPUT mode, connect it to the TRIGGER pinout and have it set to LOW, then set in to HIGHT for at least 10µs and then set it back to LOW. Shortly after this pulse is received by the sensor (TRIGGER goes from HIGHT back to LOW), the ECHO pinout will go HIGH and the sensor will send a wave at the same time. The ECHO pinout will remain in HIGH position until it receives the wave back, at that moment the ECHO will go back to LOW. Therefore you will need one more GPIO set to INPUT mode and connect it to the ECHO pinout. Then in your program, you will have to wait until it goes HIGHT, then start counting, wait until it goes back to LOW as it received the wave back and then stop the counting. The time you measure is the time a sound wave spend going to the obstacle and back to the sensor.&lt;/p&gt;

&lt;p&gt;Thanks to many clever minds who did research on speed of sound we now know, that it travels around 332 meters per second at 20°C, keep in mind that it slightly changes with different air temperature. To be more precise, use this equation to count the speed for your conditions (it assumes the air humidity is 0% and that temperature is in °C):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;speedOfSound = (331.3 + 0.606 * temperature) m/s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To tell the distance we only need to multiply the speed of sound with the measured time and divide it by two. That is because the sound wave had to go the distance twice - to the obstacle and back to the sensor. And viola! We have measured the distance! Now lets go connect the sensor to the Raspberry and write a program to count the distances for us…&lt;/p&gt;

&lt;h2 id=&quot;connecting-it-to-raspberry&quot;&gt;Connecting it to Raspberry&lt;/h2&gt;

&lt;p&gt;JSN-SR04T sensor has four pinouts. First pair is VCC and GND for obvious purpose of connecting it to 5V power and grounding it. And the second pair is TRIGGER and ECHO that we already mentioned in previous section. So simply connect the VCC to 5V, GND to GND, ECHO to a GPIO &amp;amp; TRIGGER to different GPIO. Simple.&lt;/p&gt;

&lt;h2 id=&quot;reading-the-distance&quot;&gt;Reading the distance&lt;/h2&gt;

&lt;p&gt;Now lets create a file called distance.py and write the actual code:&lt;/p&gt;

&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/python
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Import required Python libraries
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;# library for time reading time
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;RPi.GPIO&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# library to control Rpi GPIOs
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# We will be using the BCM GPIO numbering
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setmode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BCM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Select which GPIOs you will use
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Set TRIGGER to OUTPUT mode
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Set ECHO to INPUT mode
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Set TRIGGER to LOW
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Let the sensor settle for a while
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Send 10 microsecond pulse to TRIGGER
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# set TRIGGER to HIGH
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.00001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# wait 10 microseconds
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_TRIGGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# set TRIGGER back to LOW
&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Create variable start and give it current time
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Refresh start value until the ECHO goes HIGH = until the wave is send
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Assign the actual time to stop variable until the ECHO goes back from HIGH to LOW
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_ECHO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Calculate the time it took the wave to travel there and back
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;measuredTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Calculate the travel distance by multiplying the measured time by speed of sound
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;distanceBothWays&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;measuredTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;33112&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# cm/s in 20 degrees Celsius
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Divide the distance by 2 to get the actual distance from sensor to obstacle
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;distanceBothWays&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Print the distance
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Distance : {0:5.1f}cm&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Reset GPIO settings
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Run the main function when the script is executed
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can get the code also from my &lt;a href=&quot;https://gitlab.com/bambusekd-dev-blog/raspberry-measure-distance-jsn-sr04t/blob/master/distance.py&quot;&gt;GitLab&lt;/a&gt; or just clone the repo to your Raspberry.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://gitlab.com/bambusekd-dev-blog/raspberry-measure-distance-jsn-sr04t.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once you have the code ready, fire up your terminal on Raspberry, navigate to the folder with your script, point the sensor somewhere and run the script.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python distance.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If everything worked as it should, you should see message similar to this in your terminal:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Distance : 25.1cm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Good job, you have successfully measured a distance between an obstacle and your sensor!&lt;/p&gt;

&lt;h2 id=&quot;some-last-thoughts&quot;&gt;Some last thoughts&lt;/h2&gt;

&lt;p&gt;You should always bare in mind, that this sensor has limited capabilities. You have to setup it properly, make sure it points to the place whose distance you desire to measure and also that the speed of sound differs in different temperature/humidity. Also Python’s &lt;code class=&quot;highlighter-rouge&quot;&gt;time&lt;/code&gt; library is not the best tool for precise time measurement. For greater precision, I would recommend to use a C program rather than Python. But worry not, if you can tolerate some inaccuracy, then there is no problem using Python and the code provided above. Happy hacking!&lt;/p&gt;
</description>
        <pubDate>Mon, 21 Aug 2017 00:00:00 +0200</pubDate>
        <link>https://www.bambusekd.cz/dev/raspberry-measure-distance-JSN-SR04T</link>
        <guid isPermaLink="true">https://www.bambusekd.cz/dev/raspberry-measure-distance-JSN-SR04T</guid>
        
        <category>raspberry</category>
        
        <category>distance</category>
        
        <category>jsn-sr04t</category>
        
        <category>python</category>
        
        <category>electronics</category>
        
        
        <category>raspberry</category>
        
      </item>
    
      <item>
        <title>Reading temperature &amp; humidity from DHT sensors with Raspberry</title>
        <description>&lt;h2 id=&quot;dht-sensors&quot;&gt;DHT sensors&lt;/h2&gt;

&lt;p&gt;Have you ever wanted to build your own weather station? Then read further, because today we are going to look at how to measure temperature and humidity with our Raspberry! We are going to use DHT11/22 sensor, which you can get on Ebay for 1 to 4 dollars, depending on the type and accessory that comes with it. These sensors are smaller that s match box and quite accurate. There are two basic models of the sensor - DHT11 and DHT22, you can find their characteristics below. Generally DHT11 is cheaper and less accurate than DHT22, but the price of DHT22 is still so low that there is no reason why you should hesitate and not buy the 22 version.&lt;/p&gt;

&lt;p&gt;The sensor can come as a bare “mini plastic box” with 4 pinouts or it can be mounted on a board and have 3 pinouts. The difference is that the version mounted on the board already has a pull-down resistor connected to it and you don’t have to worry about a NULL pin, which doesn’t have any purpose for us, on the “bare” version. The three remaining pinouts are for power input, ground and data output, how simple! Bare in mind that if you have the version without the board, you will also need a 4.7kΩ-10kΩ resistor to make it work properly!&lt;/p&gt;

&lt;p&gt;This sensors is able to measure temperature and humidity. If you would cut the plastic box guarding the inner parts of the sensor, you would find two separate sensors there. A thermistor for temperature readings, which is simply a variable resistor that changes its resistance according to the temperature. And second sensor for humidity readings, that consists of two electrodes and a moisture holding substrate between them. This substrate sucks humidity from its surroundings - changing its conductivity, which then changes the resistance between the two electrodes.&lt;/p&gt;

&lt;h3 id=&quot;dht11&quot;&gt;DHT11&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;attribute&lt;/th&gt;
      &lt;th&gt;value&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;cost&lt;/td&gt;
      &lt;td&gt;starting on $1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;power&lt;/td&gt;
      &lt;td&gt;3-5V&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;humidity&lt;/td&gt;
      &lt;td&gt;reads values from 20% to 80% with 5% accuarcy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;temperature&lt;/td&gt;
      &lt;td&gt;reads values from 0°C to 50°C ±2°C accuracy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;reading interval&lt;/td&gt;
      &lt;td&gt;1Hz - can read values every second&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;div class=&quot;post-images&quot;&gt;
    &lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-read-temperature-humidity-DHT/dht11.jpg&quot; alt=&quot;DHT11&quot; /&gt;
    &lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-read-temperature-humidity-DHT/dht11_board.jpg&quot; alt=&quot;DHT11 on board&quot; /&gt;
&lt;/div&gt;

&lt;h3 id=&quot;dth22&quot;&gt;DTH22&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;attribute&lt;/th&gt;
      &lt;th&gt;value&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;cost&lt;/td&gt;
      &lt;td&gt;starting on $3&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;power&lt;/td&gt;
      &lt;td&gt;3-5V&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;humidity&lt;/td&gt;
      &lt;td&gt;reads values from 0% to 100% with 2-5% accuracy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;temperature&lt;/td&gt;
      &lt;td&gt;reads values from -40°C to 80°C ±0.5°C accuracy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;reading interval&lt;/td&gt;
      &lt;td&gt;0.5Hz - can read values every 2 seconds&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;div class=&quot;post-images&quot;&gt;
    &lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-read-temperature-humidity-DHT/dht22.jpg&quot; alt=&quot;DHT22&quot; /&gt;
    &lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-read-temperature-humidity-DHT/dht22_board.jpg&quot; alt=&quot;DHT22 on board&quot; /&gt;
&lt;/div&gt;

&lt;h2 id=&quot;connecting-it-to-raspberry&quot;&gt;Connecting it to Raspberry&lt;/h2&gt;

&lt;p&gt;Connecting the sensor to Raspberry is pretty simple. You can choose weather to use 3.3V or 5V output from your Raspberry to connect it to VCC pin, then connect Raspberry’s GROUND to GND and choose one GPIO to connect in to DATA pin. If you have the version not mounted on the board, connect 4.7kΩ-10kΩ resistor between VCC and DATA.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-read-temperature-humidity-DHT/circuit.png&quot; alt=&quot;Circuit&quot; class=&quot;post-image-100&quot; /&gt;
&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-read-temperature-humidity-DHT/circuit-schematics.png&quot; alt=&quot;Circuit schematics&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;reading-the-temperature&quot;&gt;Reading the temperature&lt;/h2&gt;

&lt;p&gt;Now we have everything connected and ready to read the actual temperature and humidity from our sensor. To read the actual values, we will use a python library from Adafruit, because these amazing guys already did the hard work and figured out how to translate the changing resistance of temperature/humidity sensors to human readable values. I have forked the library and you can freely download it from my &lt;a href=&quot;https://gitlab.com/bambusekd-dev-blog/raspberry-read-temperature-humidity-DHT.git&quot;&gt;Gitlab&lt;/a&gt;. Remember that you need python and git to be installed on your Raspberry to make this work! Lets go step by step and measure something finally!&lt;/p&gt;

&lt;p&gt;Open a terminal on your Raspberry and install python and git if you don’t have it yet&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo apt-get update
sudo apt-get install git build-essential python-dev python-openssl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now download the Adafruit DHT library and open the folder&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://gitlab.com/bambusekd-dev-blog/raspberry-read-temperature-humidity-DHT.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Install the library&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo python setup.py install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Great, now go to the examples folder&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd examples
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And here we go, there is a python script &lt;code class=&quot;highlighter-rouge&quot;&gt;AdafruitDHT.py&lt;/code&gt; that takes two arguments. First one is type of your sensor, so &lt;code class=&quot;highlighter-rouge&quot;&gt;11&lt;/code&gt; for DHT11 and &lt;code class=&quot;highlighter-rouge&quot;&gt;22&lt;/code&gt; for DHT22. The second one is the GPIO number (BCM number) that the DATA pinout is connected to. So if you have connected the sensor to &lt;code class=&quot;highlighter-rouge&quot;&gt;GPIO 4&lt;/code&gt;, that translates to number &lt;code class=&quot;highlighter-rouge&quot;&gt;7&lt;/code&gt; in &lt;code class=&quot;highlighter-rouge&quot;&gt;BCM&lt;/code&gt;, just execute&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo ./AdafruitDHT.py 22 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And you should see the measured temperature and humidity, something like this&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Temp=22.0*  Humidity=60.0%
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Congratz! You are now running a small weather station!&lt;/p&gt;
</description>
        <pubDate>Wed, 16 Aug 2017 00:00:00 +0200</pubDate>
        <link>https://www.bambusekd.cz/dev/raspberry-read-temperature-humidity-DHT</link>
        <guid isPermaLink="true">https://www.bambusekd.cz/dev/raspberry-read-temperature-humidity-DHT</guid>
        
        <category>raspberry</category>
        
        <category>temperature</category>
        
        <category>DHT</category>
        
        <category>electronics</category>
        
        
        <category>raspberry</category>
        
      </item>
    
      <item>
        <title>Controlling 5V relay with Raspberry</title>
        <description>&lt;h2 id=&quot;relay&quot;&gt;Relay&lt;/h2&gt;

&lt;p&gt;In todays article, we are going to have a look at an electronic component called relay. In case that you have never heard of it, it is simply a mechanical switch, that can be controlled by low level voltage (starting from usually 5V) and that can switch much higher voltages, for example the one you have at a plug at your home (110/120/220/230V depending on where you live). Inside of a relay, there is a mechanism made of a coil and a piece of metal, that can move from one side to the other. See the picture below to better imagine the inside of a relay.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-control-5V-relay/relay.jpg&quot; alt=&quot;Relay&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C&lt;/strong&gt; input is for high voltage current, so lets say for 230V. This input is normally connected by the mentioned piece of metal with &lt;strong&gt;NC&lt;/strong&gt; output, which stands for &lt;em&gt;Normally closed&lt;/em&gt;. In case you want to use a relay as a switch, which is by default in OFF mode, you will leave &lt;strong&gt;NC&lt;/strong&gt; as it is, so the voltage from &lt;strong&gt;C&lt;/strong&gt; is not going anywhere. &lt;strong&gt;NO&lt;/strong&gt; output, that stands for &lt;em&gt;Normally opened&lt;/em&gt;, is the place where you connect some electronic appliance, such as a light bulb, heating or whatever you wish to be in control of. But as we said, &lt;strong&gt;C&lt;/strong&gt; is by default connected no &lt;strong&gt;NC&lt;/strong&gt;  so whatever is connected to &lt;strong&gt;NO&lt;/strong&gt; will be OFF by default. &lt;strong&gt;C&lt;/strong&gt;, &lt;strong&gt;NC&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These three pins are those with screws on top of them, which will help you to attach the wires of an electric appliance, you will be controlling.&lt;/p&gt;

&lt;p&gt;Now comes the interesting part of controlling the relay. There is another circuit in relay, this time low level voltage, which controls if a current will flow through the coil or not. If there is a current flowing through the coil, it creates an electromagnetic(EM) field, which as we know attracts metal. As I mentioned at the beginning, there is a piece of metal inside the relay, which attracted to the coil, when the EM field is present and causes it to move from &lt;strong&gt;NC&lt;/strong&gt; position to &lt;strong&gt;NO&lt;/strong&gt; position.&lt;/p&gt;

&lt;p&gt;This circuit is controlled also by three pins. They are those traditional small pins coming out of the relay board. Those three pins are marked as &lt;strong&gt;VCC&lt;/strong&gt; - which will be connected to 5V source, &lt;strong&gt;GND&lt;/strong&gt; - which will be connected to the ground and &lt;strong&gt;IN&lt;/strong&gt; which will control if the coil should produce EM field or not.&lt;/p&gt;

&lt;h2 id=&quot;which-relay-to-buy&quot;&gt;Which relay to buy&lt;/h2&gt;

&lt;p&gt;Now when searching for a relay on the internet, you will find thousands of different types. Search for an Arduino/Raspberry 5V relay. Those 5V are important as one of the characteristics of a relay is its working voltage. RaspberryPi can give you 3.3V or 5V. Most of the relays you will find need at least 5V, but some of them can work even with 3.3V so don’t forget to check this when buying one. They come as a single relays or on a board where you have 2/4/8… of them. Their price starts at around 1$ on Ebay, so you can definitely buy at least two of them to experiment with.&lt;/p&gt;

&lt;p&gt;I have bought &lt;a href=&quot;https://www.google.cz/search?q=relay+1280+2560+ARM+PIC+AVR+DSP&amp;amp;gws_rd=cr&amp;amp;dcr=0&amp;amp;ei=0bKJWt_OKcXGwQLbmp2YCg&quot;&gt;1 channel 5V relay 1280 2560 ARM PIC AVR DSP&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-control-5V-relay/relay_image.jpg&quot; alt=&quot;Relay image&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;connecting-it-to-raspberry&quot;&gt;Connecting it to Raspberry&lt;/h2&gt;

&lt;p&gt;As I already said, Raspberry will control the relay, so we will connect it to control part of the relay - &lt;strong&gt;VCC&lt;/strong&gt; to Raspberry’s 5V output, &lt;strong&gt;GND&lt;/strong&gt; to ground and &lt;strong&gt;IN&lt;/strong&gt; will serve to control it. You might be tempted to just connect the &lt;strong&gt;IN&lt;/strong&gt; to GPIO in OUTPUT mode and switch it from high to low, but this will unfortunately not work. Relays are usually so called &lt;em&gt;GROUND/ZERO/LOW LEVEL ACTIVATED&lt;/em&gt; which means, that the &lt;strong&gt;IN&lt;/strong&gt; pin must be connected to ground to turn the relay ON and connected to 5V (or disconnected/floating) to be OFF. Therefore we will need to create a small helper circuit to be able to connect the &lt;strong&gt;IN&lt;/strong&gt; pin with ground or 5V using our Raspberry. We will need a NPN transistor, that will serve as a switch, two 10KΩ resistors, that will serve as pull-up and pull-down resistors and one 2.2KΩ resistor.&lt;/p&gt;

&lt;h3 id=&quot;pull-up--pull-down-resistors&quot;&gt;Pull-up &amp;amp; Pull-down resistors&lt;/h3&gt;

&lt;p&gt;You might be asking what is that pull-up/down resistor I just mentioned. When dealing with with sensors, relays and any other electronic components, that can be controlled, we always want to be sure that we know in which state they are (ON/OFF). This is almost always done by switching the voltage between the 5V and GROUND on some kind of &lt;strong&gt;IN/CONTROL&lt;/strong&gt; pin. However, there are situations, when you can not be sure, what voltage is on the pin, that could lead to unexpected behavior, for example if you leave the control pin just as it is, not connected to anything. It might seem like if some pin is not connected, there would be no voltage on it, so it would be the same as connecting it to ground, but no! There can be some electric charge “flying” it the air, that will “sit” on the pin and suddenly turn your circuit ON and then it can again “fly away” so your circuit can go mad switching between ON and OFF multiple times. Such a state when the pin is not connected is called &lt;em&gt;FLOATING&lt;/em&gt; state and we need to make sure that or control pins will never happen to be in such a state.&lt;/p&gt;

&lt;p&gt;Here comes the pull-up/down resistor. It is used to make sure, that in default state, there is always high voltage on the pin, e.g. 5V on the input pin (in case of pull-up) or that the pin is grounded (in case of pull-down). These resistors have high resistance, usually 10KΩ, cause we do not want to have a high current floating through them, which would be plain wasting of energy and also, we want to be able to “override” the default settings, by having an optional circuit branch, with lower resistance. Circuit always go by the way of lowest resistance, so if we let it go through such a lower resistance branch, the pull-up/down branch will be kind of ignored.&lt;/p&gt;

&lt;h3 id=&quot;control-circuit&quot;&gt;Control circuit&lt;/h3&gt;

&lt;p&gt;In our case, we want the relay to be in OFF mode by default. Therefore we will use a pull-up resistor for the &lt;strong&gt;IN&lt;/strong&gt; pin. Then we will have a transistor, serving as a switch, that will connect the &lt;strong&gt;IN&lt;/strong&gt; pin to GROUND on our demand. We want this transistor to be CLOSED by default, so we will connect a pull-down resistor to it.&lt;/p&gt;

&lt;p&gt;The transistor itself will be controlled by a GPIO in OUTPUT mode. Since a big current could potentially destroy our transistor and only low current is needed to open it, we put a 2.2KΩ resistor between the GPIO and transistor’s base. Transistors collector is connected to &lt;strong&gt;IN&lt;/strong&gt; pin and emitter to GROUND. So when the transistor is opened (GPIO will be set to HIGH) the relay will turn ON, when the transistor will be closed (GPIO set to LOW), relay will be OFF.&lt;/p&gt;

&lt;p&gt;See the image below for details on how to connect the relay to Raspberry.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-control-5V-relay/relay_control_schem.png&quot; alt=&quot;Schema&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-control-5V-relay/relay_control_bb.png&quot; alt=&quot;Breadboard schema&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;controlling-the-relay&quot;&gt;Controlling the relay&lt;/h2&gt;

&lt;p&gt;Now we have the circuit ready, so lets test it. First lets just simply use a &lt;strong&gt;gpio&lt;/strong&gt; command line program to turn the relay on and off.&lt;/p&gt;

&lt;p&gt;First lets set the GPIO to OUTPUT mode - turn on the command line and write:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpio mode 7 OUT
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s check if it is really in OUTPUT mode by writing:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpio readall
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which will show a schematics of your Raspberry with all pinouts, their number, mode and current value. Now turn the relay ON by setting the GPIO to HIGH:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpio write 7 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The relay should turn ON. Horay! Now turn it back OFF by setting the GPIO to LOW:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpio write 7 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Voila! You are now in control of the relay. But how about writing a simple python program, that would switch the relay on and off by pressing an ENTER key.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/python
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Import required Python libraries
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;RPi.GPIO&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# We will be using the BCM GPIO numbering
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setmode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BCM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Select a control GPIO
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_CONTROL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;17&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Set CONTROL to OUTPUT mode
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_CONTROL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Main function
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Start by setting the relay to OFF
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;relayState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_CONTROL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;relayState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Relay is OFF'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Repeat till the program is ended by the user
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Wait while ENTER is pressed
&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# This is a small hack, raw_input() can read keyboard input until you press ENTER and save it to a variable, but we dont care about other keys to be pressed, so we don't
&lt;/span&gt;      &lt;span class=&quot;nb&quot;&gt;raw_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
      &lt;span class=&quot;c1&quot;&gt;# Toggle the relayState value
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;relayState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relayState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Change the CONTROL output value
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO_CONTROL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;relayState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Print state to console
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;relayState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Relay is ON'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Relay is OFF'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# If the program is ended cleanup GPIOs
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;KeyboardInterrupt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Run the main function when the script is executed
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Call this file &lt;strong&gt;relay_control.py&lt;/strong&gt; and run it&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python relay_control.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Every time you press ENTER, relay will switch from OFF to ON and the other way round. Also, on every switch, there will be a message printed into the console reading &lt;em&gt;Relay is ON/OFF&lt;/em&gt;. Give it a try and enjoy creating cool projects with relays and Raspberry!&lt;/p&gt;

&lt;p&gt;The code, schema and breadboard wiring is also available on my &lt;a href=&quot;https://gitlab.com/bambusekd-dev-blog/raspberry-control-5V-relay&quot;&gt;GitLab&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Thu, 10 Aug 2017 00:00:00 +0200</pubDate>
        <link>https://www.bambusekd.cz/dev/raspberry-control-5V-relay</link>
        <guid isPermaLink="true">https://www.bambusekd.cz/dev/raspberry-control-5V-relay</guid>
        
        <category>raspberry</category>
        
        <category>relay</category>
        
        <category>electronics</category>
        
        <category>pull-up</category>
        
        <category>pull-down</category>
        
        
        <category>raspberry</category>
        
      </item>
    
      <item>
        <title>Raspberry pinouts - BCM/BOARD/wiringPi</title>
        <description>&lt;h2 id=&quot;raspberry-pinouts&quot;&gt;Raspberry pinouts&lt;/h2&gt;

&lt;p&gt;If you are going to connect some electronics to your Raspberry, no matter if to power them, control them or read dat from them, you will need to use Raspberry’s pinouts, which are those small wires going out of the raspberry in the corner. There are more versions of Raspberry (A, B, A and B in revision 2.0, A+,B+,2B,3B and som) and they vary in number and placement of their pinouts. Namely version A &amp;amp; B have 26 pinouts and all others have 40. Each A, B and the rest have different layout of their pins, so there are 3 variations in total. Let’s have a look at them!&lt;/p&gt;

&lt;h3 id=&quot;rpi-b-revision-1&quot;&gt;RPi B revision 1&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-pinouts-bcm-board-wiringpi/rpi-rev1.png&quot; alt=&quot;RPi A revision 1&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;rpi-ab-revision-2&quot;&gt;RPi A/B revision 2&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-pinouts-bcm-board-wiringpi/rpi-rev2.png&quot; alt=&quot;RPi A/B revision 2&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;rpi-ab2b3b&quot;&gt;RPi A+/B+/2B/3B&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-pinouts-bcm-board-wiringpi/rpi.png&quot; alt=&quot;RPi A+/B+/2B/3B&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;credits for images go to &lt;a href=&quot;http://www.raspberrypi-spy.co.uk/2012/06/simple-guide-to-the-rpi-gpio-header-and-pins/&quot;&gt;RaspberryPi Spy&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;gpio&quot;&gt;GPIO&lt;/h2&gt;

&lt;p&gt;If you are going to control something with your Raspberry or read data from a sensor lets say, you will need a &lt;strong&gt;general purpose input/output&lt;/strong&gt; to conect it to. These pins - &lt;em&gt;GPIO&lt;/em&gt; in short - can be set to OUTPUT or INPUT mode. In OUTPUT mode you can read HIGH or LOW (eg. 1 or 0) values on the pin depending if you brings 3.3V voltage on it for HIGH, respectively 0V for LOW. In reverse, if you set GPIO to INPUT mode, you can set it to HIGH, which means the pin will be source of 3.3V or to LOW, which means it will give you 0V.&lt;/p&gt;

&lt;h2 id=&quot;board-vs-bcm-vs-wiringpi&quot;&gt;BOARD vs. BCM vs. wiringPi&lt;/h2&gt;

&lt;p&gt;You might have noticed, that each pin has a number, but also a label, which in GPIO’s case also contains number different from the actual pin number. You always have to bare in mind, which pin numbering you are using! Those numbers on pins are reffered to as BOARD numbering, numbers mentioned in labels reffer to BCM numbering. To make it even more confusing, there is another numbering called wiringPi used in command-line utility &lt;code class=&quot;highlighter-rouge&quot;&gt;gpio&lt;/code&gt; which we will have a look at in next article. This utility offers an option to print all pin numbers with all their different numbering. So either write&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpio readall
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;in your Raspberry terminal or have a look at this screen to see all the numberings together. BCM goes for BCM, wPi goes for wiringPi and Physical goes for BOARD.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.bambusekd.cz/images/dev/raspberry-pinouts-bcm-board-wiringpi/wiringPi.jpg&quot; alt=&quot;RPi numbering&quot; class=&quot;post-image-100&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now finally, put this blog post to your favourites bar, cause you will come back here quite often before you remember the numbers by heart…&lt;/p&gt;

</description>
        <pubDate>Tue, 08 Aug 2017 00:00:00 +0200</pubDate>
        <link>https://www.bambusekd.cz/dev/raspberry-pinouts-bcm-board-wiringpi</link>
        <guid isPermaLink="true">https://www.bambusekd.cz/dev/raspberry-pinouts-bcm-board-wiringpi</guid>
        
        <category>raspberry</category>
        
        <category>pinouts</category>
        
        <category>gpio</category>
        
        <category>bcm</category>
        
        <category>board</category>
        
        <category>wiringpi</category>
        
        
        <category>raspberry</category>
        
      </item>
    
      <item>
        <title>RaspberryPi course - building garden control &amp; weather station</title>
        <description>&lt;h2 id=&quot;raspberry-course&quot;&gt;Raspberry Course&lt;/h2&gt;

&lt;p&gt;Greetings! I am very glad that you ended up on my blog, mostly focused on programming and electronics. Since you clicked on this post I guess you are probably looking for some learing material to teach yourself how to run and control your RaspberryPi, how to connect various sensors and other components to it and read data from them and how to make something cool with it. If it is really your case, then read ahead, because I have decided to write a series covering everything from seting up your Raspberry for the first time to creating a super cool garden control system running on your own Raspberry server, which will enable you to controll your water pump while checking the temperature and heigh of your water well!&lt;/p&gt;

&lt;p&gt;From now on, I will publish short articles, each focused on one major thing that you can do with your Raspberry. We will start with a setup, then learn how to write programs in Python, how to connect and read data from your sensors, then we will run a Laravel server on your Raspberry with MySQL database and finally, we will put everything together and build a remote garden control system with a weather station.&lt;/p&gt;

&lt;p&gt;Articles won’t be published on regullar basis, but you can expect aproximately one article per two weeks. Below, I am going to create a list of published and planned articles, so go ahead and click on those which have a link - those are done and wait for those wich are next in the row.&lt;/p&gt;

&lt;p&gt;If you think there is something missing or you would like me to write an article about certain sensor, do not hesitate and give me a shout here in the comments or &lt;a href=&quot;mailto:bambusekd@gmail.com&quot;&gt;mail me&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;raspberry-course-articles&quot;&gt;Raspberry course articles&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;First time starting your Raspberry&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://bambusekd.cz/dev/raspberry-pinouts-bcm-board-wiringpi&quot;&gt;Pinouts - connecting other electronics to Raspberry&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lumos! - turning on a LED with Python&lt;/li&gt;
  &lt;li&gt;Will it rain? - reading external weather API with python&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://bambusekd.cz/dev/raspberry-read-temperature-humidity-DHT&quot;&gt;Reading temperature from a DHT sensor&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://bambusekd.cz/dev/raspberry-measure-distance-JSN-SR04T&quot;&gt;Measuring distances with ultrasonic JSN-SR04T sensor&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Day or night? - Measuring light intensity&lt;/li&gt;
  &lt;li&gt;Water everywhere - humidity, rain and water level sensors&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://bambusekd.cz/dev/raspberry-control-5V-relay&quot;&gt;Turning high voltage electronics on and of - control a relay&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Flying around - read data from gyroscope&lt;/li&gt;
  &lt;li&gt;Securing your room - access system with NFC reader&lt;/li&gt;
  &lt;li&gt;Screenshoting you garden - conext a camera to RaspberryPi&lt;/li&gt;
  &lt;li&gt;Own linux server - running apache on Raspberry&lt;/li&gt;
  &lt;li&gt;Setting up Laravel on Raspberry&lt;/li&gt;
  &lt;li&gt;Putting it together - building a garden control system &amp;amp; weather station&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Enjoy reading and I hope these articles will become a starting point for your awsome Raspberry projects!&lt;/p&gt;
</description>
        <pubDate>Tue, 01 Aug 2017 00:00:00 +0200</pubDate>
        <link>https://www.bambusekd.cz/dev/raspberry-course-building-garden-control-weather-station</link>
        <guid isPermaLink="true">https://www.bambusekd.cz/dev/raspberry-course-building-garden-control-weather-station</guid>
        
        <category>raspberry</category>
        
        <category>python</category>
        
        <category>php</category>
        
        <category>laravel</category>
        
        <category>electronics</category>
        
        
        <category>raspberry</category>
        
      </item>
    
      <item>
        <title>Život v dešti</title>
        <description>&lt;p&gt;Bude tomu za chvíli už něco kolem dvou a půl měsíců, co trvá mé pracovní anglické dobrodružství. Až na trojici výletů do Londýna, Paříže a Dublinu se tady ovšem každý můj den podobal skoro na chlup tomu předchozímu a abych pravdu řekl, někdy je to opruz. Abych vám přiblížil co tady vlastně dělám, bude stačit když popíšu jeden takový průměrný den, ten si vynásobte sedmdesáti a máte zhruba představu o mém celém anglickém působení.&lt;/p&gt;

&lt;p&gt;V práci začínáme oficiálně v devět ráno. Vzhledem k tomu, že nemáme žádné píchačky a každý si tam chodí jak chce , propracoval jsem se od mých příchodů kolem 8:45 v prvních dvou týdnech, kdy krom mě a šéfa v kanclu ještě nikdo nebyl až k aktuálnímu času příchodů, zhruba okolo 9:20, většinou to záleží na provozu, počasí a taky jestli si ráno odbydu svých patnáct minut na záchodě ještě doma nebo až v práci. Abych stíhal, stával jsem původně v 8 hodin, ale jak se posunoval čas mých příchodů do práce, posunul se taky čas mého vstávání. Vzhledem k tomu, že jsem docela agilní co se týče vypakování se z baráku, jsem schopen vzbudit se 8:40 a 45 už být na cestě, takže to takhle nějak většinou taky bývá. Každé ráno do baťohu sbalím myš, nějakou sladkost, musli tyčinku na snídani, jedny suché trenky a ponožky kdyby náhodou chcalo a já byl promáčený až na řiť, protože tomu ver ,že se mi to už několikrát stalo, čapnu kolo, nasadím polskou reflexní vestu kterou jsem dostal v Estonsku a vyrážím na kole směr centrum. Autobusem moc nejezdím, protože to stojí těžké háky a už i tak tady občas na večeři okusuju omítku, takže kolo, které jsem fásl od jednoho z šéfíků využívám každý den a šetří mi dost peněz a hlavně, co bych neudělal pro své zdraví že. Co se městské logistiky týče, už se tady docela vyznám, po levé straně jsem si taky zvykl jezdit, i že brzdy jsou naopak už si pamatuju, akorát přednost zleva mi sviňa pořád moc nepasuje a se občas stane, že na mě někdo trubne, protože si to jak pán prosvištím křižovatkou, když si myslím, že mám přednost. Ale abych na sebe nebyl tak tvrdý, Angličani jezdí obecně jako prasata. Začíná to u cyklistů, kteří jezdí na červenou a všemi směry jak se jim zachce, pokračuje to u nervózních řidičů osobáků co pořád troubí a končí u posratých taxikářů, kteří naschvál blokujou krajní pruh pro cyklisty, který je skoro na každé cestě a jedou tak, že je tím pruhem prostě nepodjedete protože by to skončilo buď uražením zrcátka nebo některé z vašich končetin. Cesta mi trvá asi 15 minut a je většinou poklidná, až tedy na případy kdy prší. Pod pojmem prší myslím lije, protože tady prší pořád. Jak už jsem předeslal, několikrát se mi stalo, že jsem vyjel za sucha a do práce dojel promočený jak pomyvač po šichtě. Jednou jsem si musel dokonce dojít koupit nové trenky a ponožky abych měl aspoň něco suchého a od té doby vždycky vozím v báglu rezervu. Vzhledem k tomu že blatníky nemám a ani vlastně  vzhledem ke kvantitě vody kolem vás při dešti nemají moc smysl, jsem pořád celý od bláta, ale kalhoty ve kterých jezdím mám jenom na kolo, takže je to cajk.&lt;/p&gt;

&lt;p&gt;Pracuju v moc pěkném kanclu přímo v centru města. Náš Cleversteam tvořím já, šéf co je zároveň i programátor, ještě jeden programátor a dva designéři. Kupodivu tam nejsem nejmladší, což mě přivádí k myšlenkám, že už mám fakt asi taky roky… Vedle nás sedí ještě 4 lidi z firmy co podporuje startupy a tak nějak pod nás patří a občas do kaclu dochází CEO a dva akcionáři/šéfi Campleaders potažmo celé Smaller Earth skupiny pod kterou patříme i my. Všechno jsou to moc pohodoví lidi a i když je v kanceláři většinou ticho, tak když už se začnou bavit, tak to stojí za to. Už jsem se trochu adaptoval i co se týče porozumění přízvukům, které má každý úplně jiný, takže si troufnu říct, že tak 50 procent veškeré konverzace už chytám :D Máme malou kuchyňku s kávo/čajo varem, který frčí pořád na plné koule, protože Angličani vychlemtají během dne hektolitry ať už toho nebo toho, ledničku a dva záchody. To poslední jmenované mi dělá největší radost, protože není nad to když člověk může v klidu dělat co potřebuje a není nervózní, že za dveřma stojí někdo další v řadě… Jediné co mě štve je děsná kosa, kterou tam máme. Všichni jsou pořád nemocní, kašlou, ale chodí v krátkém tričku a to pitomé topení prostě nezapnou i přesto, že se tam sami klepou Parkinson hadra. Já se docela dlouho držel a i díky každodenní malé dávce slivovice po práci jsem si udržoval zdravého duchaí, nic ale netrvá věčně a tak jsem minulý týden taky chytl nějakou rýmičku. Proti zimě se každodenně snažím bojovat na všech frontách, kancl je velký takže než se vytopí, asi hoďku to trvá. Zmrzlé ruce si chodím ohřívat pod fukar na záchodech, jelikož třeba pod vodou to nejde, protože kohoutky jsou rozdělené a jak už jsem psal, z jednoho teče skoro pára a z druhého se dělají rampouchy. V neposlední řadě pak také přispívám k velké spotřebě čaje, protože to je jediný zdroj tepla, který dokážete dostat do sebe. Taky je fajn, že jako vedlejší efekt často chodíte čůrat, takže se aspoň trochu protáhnete od práce.&lt;/p&gt;

&lt;p&gt;Šichty kancelářského plebsu jsou osmihodinové, teoreticky. Většinou jsem to já kdo tam opravdu zůstává celých 8 hodin a pak šéf, který se mi svěřil, že někdy je v práci už ve 4 ráno a zůstává tam až do večera, je to trochu workoholik, co si budem povídat… Někdy kolem poledne si vždycky zajdu na oběď. Typický anglický oběd je sandwich a tak v tom samozřejmě frčím taky. Většinou si skočím do Tesca, které máme za rohem na “Meal deal” což je sendvič, pití a nějaké ovoce nebo brambůrky k tomu. Stojí to 3 anglokačky, lepší poměr výkon/cena neseženete. Že jsem ale pořád chlapec ve vývinu a mívám docela hlad, takže poslední dobou si k meníčku kupuju ještě něco z “teplé přihrádky”. Je to prostě regál, kde je udělané jídlo, které se udržuje teplé. Vždycky mě fascinovaly masové koláče a pečivo s masem anglické receptury, kterého tu mají fůru, takže docela ujíždím na tomhle, páč to moc nestojí, je to docela dobré a hlavně teplé a když zrovna není slina na pečivo, tak vždy sahám po kuřecích nugetkách nebo rybích bobcích, kouscích nebo whatever to je. Mimochodem, vyřídil jsem si Tesco klubovou kartičku a se smutkem jsem zjistil, že i když mám nasbíraných přes 200 bodů, jakési kupóny dostanu až v únoru či kdy, prostě poser, kdyby aspoň dávali nějaké pánvičky, šmouly nebo jakoukoliv jinou plyšovou věc stylu oživlé mutované zeleniny kdesi od Černobylu s magickýma schopnostma, které můžete sbírat , ale oni nic, vůbec nic…&lt;/p&gt;

&lt;p&gt;Ve firmě dělám ze všeho nejvíc nejvíc front-end vývoj webovek, pro nezasvěcené dělám prostě webové stránky, snažím se, aby vypadaly tak jak je navrhnou designeři, fungovaly na nich všechny udělátka, skákaly veselé obrázky a texty a tak vůbec. Vzhledem k tomu, že jsem se očividně osvědčil, tak hned po prvním týdnu jsem dostal na starost první projekt, kterých mám teď souběžně asi pět a vzhledem k tomu, že nikdo jiný nemá čas, tak je všechny nějak tak vedu, což mi docela masíruje ego, co s projektama bude až odejdu netuším, ale myslím, že na stážistu slušná pozice, škoda akorát že mi nic neplatí… Hned od začátku jsem byl plně integrován do týmu, takže se účastním i hodinových porad, které jsou na pořadu týdne vždy v pondělí v 11, takže mám docela přehled jak to všechno vlastně ve firmě funguje. Ovšem největší sranda je, že je to všechno úplně jinak než nám to vykládají ve škole a takováhle dvouměsíční praxe mi toho dala víc než celý bakalář na našem prestižním VUT , které tady samozřejmě nikdo nezná a vlastně ani nikoho moc nevzrušuje jestli nějaké školy máte, spíš jde o to co umíte, možná i proto tady nikdo tituly nepoužívá a to ani ve formální korespondenci nebo na vizitkách…&lt;/p&gt;

&lt;p&gt;Většinou z práce odcházím po páté. Kolem šesté jsem doma a jsem rád že jsem rád. Se sedavou prací jsem moc zkušeností neměl a překvapivě mě to zmáhá víc než prostátá šichta v kuchyni u krájení okurek. Než jsem tady dojel, představoval jsem si jak volné večery budu věnovat učení se novým věcem, pilováním španělštiny a ruštiny, cvičení a tak. Každopádně můj večerní program je asi takový, že si přečtu všechny zprávy, zavolám si se svou Peťou, udělám večeři, podívám se na nějaký seriál, něco malinko si přečtu a už je jedna ráno jsem kaput a je čas jít spát. Nikam ven moc nechodím, jelikož bych se nedoplatil, i když bylo pár výjimek a taky mé výletky stály docela dost, takže bylo třeba šetřit a až na malé procházky jsem se nikam moc z baráku nedostal.&lt;/p&gt;

&lt;p&gt;Shrnuto podtrženo je to tady trochu nudný stereotyp. Dost věcí mi tu chybí, Peťa, levné pivo, pořádná strava, pořádná sprcha a taky pořádná postel. Tyvole takovou postel jste nikdo nezažili, každé ráno kontroluju, jestli některé péro přes noc neprolítlo mojim břichem nebo hlavou, protože se jednak zevnitř postele ozývají divné zvuky a každou chvíli někde tlačí nějaká ven se deroucí pružina, takže kvalita spánku je nastavená hodně nízko. Abych nebyl ale jen kritický tak si myslím, že profesně mi to dává víc než cokoliv předtím a taky, že kolonka vývojář v anglické společnosti nebude vypadat špatně v mojem sívíčku. Takže doufám, že i přes všechno co jsem tady musel přetrpět se to nakonec oplatí.&lt;/p&gt;
</description>
        <pubDate>Mon, 15 Dec 2014 00:00:00 +0100</pubDate>
        <link>https://www.bambusekd.cz/blog/anglie-zivot-v-desti</link>
        <guid isPermaLink="true">https://www.bambusekd.cz/blog/anglie-zivot-v-desti</guid>
        
        <category>cestování</category>
        
        <category>anglie</category>
        
        <category>erasmus</category>
        
        
        <category>cestování</category>
        
      </item>
    
  </channel>
</rss>
