<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bhavesh Kakwani</title>
    <description>The latest articles on DEV Community by Bhavesh Kakwani (@bhav).</description>
    <link>https://dev.to/bhav</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F378146%2F8d87e450-61c1-4279-a317-21f231bbcdac.jpg</url>
      <title>DEV Community: Bhavesh Kakwani</title>
      <link>https://dev.to/bhav</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhav"/>
    <language>en</language>
    <item>
      <title>An ESP32 walks into a bar - Jokes web API MicroPython tutorial</title>
      <dc:creator>Bhavesh Kakwani</dc:creator>
      <pubDate>Mon, 21 Feb 2022 15:15:47 +0000</pubDate>
      <link>https://dev.to/bhav/an-esp32-walks-into-a-bar-jokes-web-api-micropython-tutorial-1gno</link>
      <guid>https://dev.to/bhav/an-esp32-walks-into-a-bar-jokes-web-api-micropython-tutorial-1gno</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted &lt;a href="https://bhave.sh/micropython-urequests/"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(If this is your first time here, check out my first MicroPython tutorial. Use it to get acquainted with the Wokwi simulator so that you are prepared to dive into this tutorial)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This week, our MicroPython tutorial flies away and connects to the web to fetch data from an API! While filming it, I ran into a temporary issue with the Wokwi simulator (thankfully it's fixed now, they were quick 🙂) so I ported the code to an actual ESP32 to continue the tutorial. Crisis averted, whew! What we can learn from this experience is that Wokwi is actually a perfect prototyping tool because I had to make zero changes to the code for it to work on real hardware. Amazing!&lt;/p&gt;

&lt;p&gt;Hope you enjoy, and click follow down below and on my &lt;a href="https://youtu.be/73RvDEKb_U4"&gt;YouTube channel&lt;/a&gt; to get notified when new tutorials are out! I release them about once a week. As always, if you have questions or just wanna chat, hit me up here or on &lt;a href="https://twitter.com/beyond_sol"&gt;Twitter&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/73RvDEKb_U4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>micropython</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
    <item>
      <title>Secrets of MicroPython: How to detect motion</title>
      <dc:creator>Bhavesh Kakwani</dc:creator>
      <pubDate>Sun, 16 Jan 2022 16:18:07 +0000</pubDate>
      <link>https://dev.to/bhav/secrets-of-micropython-how-to-detect-motion-1k0e</link>
      <guid>https://dev.to/bhav/secrets-of-micropython-how-to-detect-motion-1k0e</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published &lt;a href="https://bhave.sh/micropython-detect-motion/"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(If this is your first time here, check out my &lt;a href="https://bhave.sh/micropython-blink-led/"&gt;first MicroPython tutorial&lt;/a&gt;. Use it to get acquainted with the Wokwi simulator interface so that you are prepared to dive into this tutorial).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As humans, we rely a lot on our eyesight to detect movement in our environment. But there's a core requirement to that - the environment must be illuminated by light for us to see anything. That seems like an obvious requirement, until you realize that devices like smart light-switches are able to detect your movement in a pitch dark hallway and turn on the lights for you. How do they do that? Today we'll learn how, and even build our own smart light using MicroPython!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RLL4p54M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/799ni7ctue8zhhh2ye6x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RLL4p54M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/799ni7ctue8zhhh2ye6x.png" alt="Image description" width="880" height="996"&gt;&lt;/a&gt;Enemy cones of vision in the 1998 stealth classic Metal Gear Solid. &lt;em&gt;Jetijones, CC BY 3.0, via Wikimedia Commons&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;"Huh? Who's there?" If you've played stealth video games, you'll recognize the cone of vision that visualizes whether an enemy character can spot you moving. The cone shape accurately captures the sensitivity of our vision - movement that is closer to the centre of the cone or closer to the person is easier to notice than movement that is far from the person or from their line of sight. (It's a bit more complex than that because we have extra sensitivity to high frequency motion in our peripheral vision, but I won't get into it).&lt;/p&gt;

&lt;p&gt;Turns out this cone of vision applies to machines as well! In every smart light-switch or automatic door system, there is a sensor known as a PIR, or passive infrared sensor. PIR sensors measure infrared light and they too have a 'cone of vision' i.e. they can sense movement up to a certain distance and angle, just like video game characters. What makes them different is that they can see in the dark.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JsCF3DXH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhk8nty5wtbljbmr1sr4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JsCF3DXH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhk8nty5wtbljbmr1sr4.png" alt="Image description" width="768" height="1024"&gt;&lt;/a&gt;Automatic light-switch with PIR sensor at the top. Now you know why it looks so weird. &lt;em&gt;Z22, CC BY-SA 4.0, via Wikimedia Commons&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;How? We humans (and animals too) are constantly radiating out energy from our bodies in the form of infrared light. You are a magnificent, radiant being - literally. When we step in front of a PIR sensor, it sees us in infrared regardless of whether the room is dark or lit. The sensor converts this infrared light into an electrical signal, which is conditioned by internal circuitry to output a digital signal - a hight voltage or '1' when motion is detected, and low voltage or '0' when no motion is detected.&lt;/p&gt;

&lt;p&gt;Well, that's convenient. We can use this to build our own smart light-switch and get rich! Let's &lt;a href="https://wokwi.com/arduino/new?template=micropython-pi-pico"&gt;fire up the Raspberry Pi Pico simulator in Wokwi&lt;/a&gt; and give it a try. Click the 'plus' button at the top of the simulator and select the PIR Motion Sensor component. It has 3 pins - VCC, GND and OUT. As usual, we'll refer to the &lt;a href="https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf"&gt;Pi Pico pinout&lt;/a&gt; and find that VCC should be connected to the 3.3V supply at pin 36. GND can be connected to any ground pin - I chose pin 23 this time. OUT can be connected to any of the GPIO pins - I chose GP20 on the right side. Let's wire it up in the simulator pane!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qrTv5juc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uydab7c1uhlxe4zw5wnz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qrTv5juc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uydab7c1uhlxe4zw5wnz.png" alt="Image description" width="534" height="664"&gt;&lt;/a&gt;PIR sensor connected to the Raspberry Pi Pico&lt;/p&gt;

&lt;p&gt;Before we get fancy and try to control a light, let's first get a feel for the output from the PIR sensor. In the Wokwi editor, we'll write a MicroPython program to just print out the digital value being output by the sensor. As always, we'll use the machine and time libraries to accomplish this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pqYa_cgO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4h8vk25jgs6t5cie6u6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pqYa_cgO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4h8vk25jgs6t5cie6u6.png" alt="Image description" width="542" height="384"&gt;&lt;/a&gt;Code for printing out PIR sensor data&lt;/p&gt;

&lt;p&gt;In the setup phase, we define the pin GP20 as an input pin so it can read the digital output of the PIR. In the main loop, we repeatedly read the value on that pin and print it out to the console. Then the code is idle for 0.5 seconds before reading the PIR again. Run the simulation and click 'Simulate motion' on the PIR sensor to see how its value changes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ALs1lgrj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgvug6itfoolh8e6jie6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ALs1lgrj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgvug6itfoolh8e6jie6.gif" alt="Image description" width="554" height="1354"&gt;&lt;/a&gt;Data from the PIR sensor when it detects motion&lt;/p&gt;

&lt;p&gt;As expected, it outputs a '0' all the time and outputs a '1' when motion is detected. However, it only outputs a '1' for about 5 seconds. Imagine trying to switch on a light in your room directly from this sensor data - you'd have to move in front of it every 5 seconds just to keep the light on! It would be pretty annoying. Our code needs to account for this and keep the light on for much longer. In a real light-switch, that time may be something like 1 - 2 hours. But to test out our code, let's just set it to 10 seconds and see if it works (unless you want to stare at your screen for an hour just to find out your code doesn't work!).&lt;/p&gt;

&lt;p&gt;Here are the requirements: (a) if someone moves in front of the light, switch it on for 10 seconds, and (b) if someone moves again within that 10 second time-frame, refresh the countdown again so the light stays on for 10 seconds. Let's create a flowchart to help visualize the program.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oRjGrraR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thu4vbaaehl6kgf91tb2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oRjGrraR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thu4vbaaehl6kgf91tb2.png" alt="Image description" width="278" height="853"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before starting the main loop, we create a variable called countdown which we'll use for keeping the light on for a set time. We then start the program's main loop by reading the PIR sensor. If it returns '1', i.e. motion was detected, then we switch on the LED and set the countdown variable to 10 seconds. The code sleeps for 0.5 seconds and we account for that by subtracting the time from the countdown time. We keep repeating this until the countdown reaches 0, at which point we know that no more motion was detected so we can switch off the LED.&lt;/p&gt;

&lt;p&gt;If this code works for the 10-second case, we know it should also work for the 1 hour case - we'll simply just have to change the countdown to 3600 seconds at that point, and we'll have a real (simulated) smart light! Let's modify our code from before to look like below. Remember from the &lt;a href="https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf"&gt;Pico pinout diagram&lt;/a&gt; that the onboard LED is on pin GP25.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZohhRS9v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4a1us3k30xxo98g7gzg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZohhRS9v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4a1us3k30xxo98g7gzg.png" alt="Image description" width="880" height="819"&gt;&lt;/a&gt;Code for controlling an LED based on motion detected by the PIR sensor&lt;/p&gt;

&lt;p&gt;Press the 'play' button in the simulator to try out your code! You'll see now that the LED stays on for 10 seconds even though the PIR sensor outputs '1' for only 5 seconds. And if you change the ON_TIME constant from 5 seconds to 3600 seconds, the LED will actually stay on for an hour in the simulation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jBw-JE_---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yuw4xvj9wzqdxv3fnat.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jBw-JE_---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yuw4xvj9wzqdxv3fnat.gif" alt="Image description" width="600" height="933"&gt;&lt;/a&gt;I wanted to put an hour-long gif here but they wouldn't let me&lt;/p&gt;

&lt;p&gt;There you have it! A fully auto-magic smart light, in just a couple dozen lines of MicroPython. What a time to be alive. For today's extra challenge, let's combine your knowledge of motion sensing and temperature sensing &lt;a href="https://bhave.sh/micropython-measure-temperature/"&gt;from the previous tutorial&lt;/a&gt;. You're going to build the sensor system for a thermostat! Your system must switch on the LED for 10 seconds if and only if (a) motion is detected, and simultaneously (b) the temperature is below 18 °C. Believe it or not, this is more or less how a smart thermostat with occupancy sensing works.&lt;/p&gt;

&lt;p&gt;Good luck on the challenge question, and please reach out to me on &lt;a href="https://twitter.com/beyond_sol"&gt;Twitter&lt;/a&gt; if you'd like to ask me questions, give feedback, discuss the challenge solution or just casually chat about the meaning of life. And do follow/subscribe if you'd like more of my MicroPython tutorials :) I'm looking forward to your messages! Sayonara!&lt;/p&gt;

</description>
      <category>micropython</category>
      <category>python</category>
      <category>tutorial</category>
      <category>raspberrypipico</category>
    </item>
    <item>
      <title>Secrets of MicroPython: How to measure temperature</title>
      <dc:creator>Bhavesh Kakwani</dc:creator>
      <pubDate>Fri, 07 Jan 2022 16:37:43 +0000</pubDate>
      <link>https://dev.to/bhav/secrets-of-micropython-how-to-measure-temperature-498k</link>
      <guid>https://dev.to/bhav/secrets-of-micropython-how-to-measure-temperature-498k</guid>
      <description>&lt;p&gt;&lt;em&gt;This tutorial was originally published &lt;a href="https://bhave.sh/micropython-measure-temperature/"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(If this is your first time here, check out my &lt;a href="https://bhave.sh/micropython-blink-led/"&gt;first MicroPython tutorial&lt;/a&gt;. Use it to get acquainted with the Wokwi simulator interface so that you are prepared to dive into this tutorial).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Is it just me, or is it getting hot in here? I better go check the thermometer to find out. But have you ever wondered how a digital thermometer actually works? Many of us are taught in school how mercury thermometers tell the temperature, but most of us don't ever learn the magic/physics behind digital temperature sensors. In this tutorial, you will understand how temperature is measured by microcontrollers and you will use the power of MicroPython to do it yourself!&lt;/p&gt;

&lt;p&gt;Whether you want to check for a fever, monitor the conditions in a greenhouse or protect your satellite in the freezing cold of outer space, you need to measure the temperature of your system. The most popular sensor for measuring temperature is called a thermistor. A thermistor is a component that has the peculiar property that its resistance changes with temperature. If you put a thermistor into a circuit, you can measure its change in resistance and use that to calculate its temperature. Pretty neat!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t9ghh_Z2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/suwhwii4hj531r6v9oms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t9ghh_Z2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/suwhwii4hj531r6v9oms.png" alt="A thermistor" width="581" height="524"&gt;&lt;/a&gt;A thermistor (Ansgar Hellwig, CC BY-SA 2.0 DE, via Wikimedia Commons)&lt;/p&gt;

&lt;p&gt;Luckily for us, we don't have to buy a physical thermistor to try this out. Our trusty simulator, Wokwi, has a thermistor component available for us to play with. Let's start a &lt;a href="https://wokwi.com/arduino/new?template=micropython-pi-pico"&gt;new MicroPython project&lt;/a&gt; on Wokwi. Click on the 'plus' button at the top of the simulation pane, scroll down and click on 'Analog Temperature Sensor' to add it to the schematic. You will notice that this sensor module has 3 pins: GND, VCC and OUT.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ChrwB6xD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4586tn6pdp2kogpwpqbi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ChrwB6xD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4586tn6pdp2kogpwpqbi.gif" alt="The temperature sensor module's 3 pins: GND, VCC and OUT" width="600" height="247"&gt;&lt;/a&gt;The temperature sensor module's 3 pins: GND, VCC and OUT&lt;/p&gt;

&lt;p&gt;The VCC and GND pins need to be provided with power supply (3.3V output pin) and ground, respectively, from the Raspberry Pi Pico. To understand what the OUT pin is for, let's take a closer look at the components present on this sensor module.&lt;/p&gt;

&lt;p&gt;You see the black 'bead' on the left of the module? That's the thermistor. If you look slightly to the left of the 3 pins, there is a small resistor there with a resistance of 10kΩ, i.e. 10000 ohms. The thermistor and the 10k resistor are connected in series, with the OUT pin connected between them. Here is a schematic of the module to show the circuit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aMrqsk00--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ystlmgmo5skumx8ajczc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aMrqsk00--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ystlmgmo5skumx8ajczc.png" alt="Schematic of the temperature sensor module" width="736" height="276"&gt;&lt;/a&gt;Schematic of the temperature sensor module &lt;/p&gt;

&lt;p&gt;This type of circuit is called a voltage divider, because the voltage you measure at the OUT pin is a fraction of the power supply voltage VCC. In other words, the voltage VCC gets divided between the resistor and the thermistor. The fraction depends on the ratio between the resistance of the thermistor and that of the resistor (i.e. 10kΩ). Using this, and the fact that the thermistor's resistance itself depends on temperature, we can back-calculate the temperature by measuring the voltage on the OUT pin.&lt;/p&gt;

&lt;p&gt;Wait a second. Does that sound familiar? If you read the &lt;a href="https://bhave.sh/micropython-read-knob/"&gt;previous tutorial&lt;/a&gt;, you'll remember that we measured a fraction of the supply voltage then too! The only difference is that then we were measuring a potentiometer and here we want to measure a thermistor. To measure voltage across the thermistor, we need to use the ADC on the Pico. Let's look at our handy &lt;a href="https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf"&gt;Pico pinout diagram&lt;/a&gt; to find an ADC pin.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xk3tcCrr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7ktjm24dc8v3rsvbups.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xk3tcCrr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7ktjm24dc8v3rsvbups.png" alt="There are 3 ADC pins available on the right side of the Pico: GP26, GP27 and GP28." width="880" height="290"&gt;&lt;/a&gt;There are 3 ADC pins available on the right side of the Pico: GP26, GP27 and GP28. &lt;/p&gt;

&lt;p&gt;We can use either of those 3 pins for our ADC. I am going to choose ADC0, i.e. pin GP26. As in the previous tutorial, we'll connect the sensor's VCC pin to the 3V3 pin at pin 36. And you can connect the GND pin to any ground pin on the Pico; I chose pin number 3. Let's wire it up in the simulator!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WzrYNfCZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90vvd77hyrerq1ye83ub.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WzrYNfCZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90vvd77hyrerq1ye83ub.png" alt="Temperature sensor's pins connected to the Pico" width="880" height="745"&gt;&lt;/a&gt;Temperature sensor's pins connected to the Pico &lt;/p&gt;

&lt;p&gt;Let's read some data from the temperature sensor to get an intuition for how it changes with temperature. We will write a basic MicroPython script to read the data using the ADC, exactly as in the &lt;a href="https://bhave.sh/micropython-read-knob/"&gt;previous tutorial&lt;/a&gt;. Type this into the code editor on Wokwi.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i1f1_2In--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ytmqc8emr6yyoeee34y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i1f1_2In--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ytmqc8emr6yyoeee34y.png" alt="Code for reading and printing data from the ADC" width="772" height="412"&gt;&lt;/a&gt;Code for reading and printing data from the ADC&lt;/p&gt;

&lt;p&gt;Run the script by pressing the 'play' button at the top of the simulator. By default, the temperature in the simulation environment is constant at a pleasant 24 °C so you will see the same ADC value being output over and over again in the console. To change the temperature, click on the temperature sensor while the simulation is running and you should see a slider. Play around with the temperature and notice the response in the ADC values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ex-7zrbA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6pxje3734i9pm03go54k.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ex-7zrbA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6pxje3734i9pm03go54k.gif" alt="Sweeping the temperature and observing the ADC output" width="800" height="966"&gt;&lt;/a&gt;Sweeping the temperature and observing the ADC output&lt;/p&gt;

&lt;p&gt;Interesting! When we decrease the temperature, the ADC value increases, and vice versa. That's because this is an NTC, or negative temperature coefficient thermistor. It's a fancy way of saying that the resistance of the thermistor decreases when its temperature increases.&lt;/p&gt;

&lt;p&gt;How &lt;strong&gt;exactly&lt;/strong&gt; does its resistance vary with temperature, though? This is where we would get into some gnarly arithmetic. Since you'd (probably) rather not relive your high-school math class trauma, I won't spend any time deriving the formula between temperature and ADC data. But I do really enjoy showing people stress-inducing math formulas, so I've written it out here for you to see the beautiful horror I shielded you from.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_JAdWFp2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2mabw5hv4s2mb1gnqk8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_JAdWFp2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2mabw5hv4s2mb1gnqk8.png" alt="Read it and weep" width="880" height="289"&gt;&lt;/a&gt;Read it and weep &lt;/p&gt;

&lt;p&gt;We do have to put this formula in our code, so let's focus on understanding the two key parameters here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The 'x' at the bottom refers to the value we get from the ADC.&lt;/li&gt;
&lt;li&gt;The β (beta) value is different for different thermistors, and it represents a thermistor's exact relationship with temperature.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;The astute among you may have also recognized the 65535 in the denominator, which we saw in the previous tutorial. It's equivalent to (2^16 - 1), which is the maximum value that the 16-bit ADC on the Pico will output. The even more astute may recognize the number 273.15, which is the constant factor for converting temperature between Celsius and Kelvin. And the astutest of all will notice that the number 298.15 is just the temperature 25 °C converted to Kelvin. But none of that really matters for writing the MicroPython program, so don't stress&lt;/em&gt; 😌&lt;/p&gt;

&lt;p&gt;Neat! We now have a way to convert ADC values, i.e. 'x' in the formula above, into a temperature in Celsius. The β value for this particular simulated thermistor is &lt;strong&gt;3950&lt;/strong&gt;. Let's use this to create a function in MicroPython that converts ADC output to temperature. We will use this function in the main loop to convert the ADC readings and print out the temperature in Celsius. Modify your code to add these two new features, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KoPscr4L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hchz5wt9i1f9k0vafjkm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KoPscr4L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hchz5wt9i1f9k0vafjkm.png" alt="Code for reading thermistor data with the ADC, converting it to Celsius, and printing it out" width="880" height="570"&gt;&lt;/a&gt;Code for reading thermistor data with the ADC, converting it to Celsius, and printing it out&lt;/p&gt;

&lt;p&gt;We have added a new import, the log function from the math library. We then define the constants β and the Kelvin constant. Next, we define the function for the formula I showed you before. It looks a lot nicer when fit into one line of code, doesn't it? No? OK. Just be careful to put all the brackets in the right places. The rest of the code should look very similar, with the main difference being that we're now calling our &lt;em&gt;adc_to_celsius&lt;/em&gt; function in the main loop and printing out its value, in addition to the raw value from the ADC.&lt;/p&gt;

&lt;p&gt;Let's run the code and see the output we get in the console. We should see the raw ADC value printed on the left side and the converted Celsius value on the right side.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x5Pj0hcX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xp2gfs7z2b7gvc1cb3nw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x5Pj0hcX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xp2gfs7z2b7gvc1cb3nw.gif" alt="How to bake a simulated Raspberry Pi Pico" width="800" height="938"&gt;&lt;/a&gt;How to bake a simulated Raspberry Pi Pico&lt;/p&gt;

&lt;p&gt;Not bad! Our conversion formula seems to convert the ADC value to temperature quite accurately as there's good correlation between the input temperature and the output. But it's not perfectly precise, is it? You may notice that it's only accurate to 1 decimal place. Let's add a small modification to the code to round the Celsius value to 1 decimal place before printing it out. Luckily for us, there is already a &lt;strong&gt;round&lt;/strong&gt; function built-in in MicroPython's standard library. Let's modify line no. 19 to use this function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hjKcdEsl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/af9onmirpctazg66ripz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hjKcdEsl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/af9onmirpctazg66ripz.png" alt="Rounding the output of the adc_to_celsius function to 1 decimal place" width="880" height="32"&gt;&lt;/a&gt;Rounding the output of the adc_to_celsius function to 1 decimal place&lt;/p&gt;

&lt;p&gt;And that's it! We just built our own digital temperature sensor using a thermistor and the ADC on the Pico. You were very brave for pushing through the math and making it to the end of this tutorial - congratulations!&lt;/p&gt;

&lt;p&gt;As always, I have a small add-on challenge for you that will improve your MicroPython skills. Imagine this temperature sensor was being used at a factory where the temperature must be maintained between 15 °C and 35 °C for safety. If the temperature goes outside this safe range, a siren (red LED) should start blinking to alert everyone! Try modifying our project to add this capability. Good luck, and please comment below or reach out to me on &lt;a href="https://twitter.com/beyond_sol"&gt;twitter&lt;/a&gt; if you have any questions or if you'd like to show me your solution 🙂 I'm looking forward to it!&lt;/p&gt;

&lt;p&gt;If you found this interesting, do follow/subscribe to be notified of new tutorials. I release tutorials once a week.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This tutorial was originally published &lt;a href="https://bhave.sh/micropython-measure-temperature/"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>micropython</category>
      <category>circuitpython</category>
      <category>tutorial</category>
      <category>hardware</category>
    </item>
    <item>
      <title>Secrets of MicroPython: How to read a knob</title>
      <dc:creator>Bhavesh Kakwani</dc:creator>
      <pubDate>Mon, 03 Jan 2022 17:12:45 +0000</pubDate>
      <link>https://dev.to/bhav/secrets-of-micropython-how-to-read-a-knob-l7e</link>
      <guid>https://dev.to/bhav/secrets-of-micropython-how-to-read-a-knob-l7e</guid>
      <description>&lt;p&gt;&lt;em&gt;This tutorial was originally published &lt;a href="https://bhave.sh/micropython-read-knob/"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(If this is your first time here, check out my &lt;a href="https://bhave.sh/micropython-blink-led/"&gt;first MicroPython tutorial&lt;/a&gt;. Use it to get acquainted with the Wokwi simulator interface so that you are prepared to dive into this tutorial)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Knobs are a universal way of controlling devices - you use them on stoves to control the temperature, on speakers to control the volume, and on Russian spacecraft to control...space stuff. The point is, they're charming and an absolute joy to use. But have you ever wondered how they work? How do these devices &lt;em&gt;know&lt;/em&gt; how far you've turned the knob?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y-NjGjlR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azbhg0qbzpj37hef5k7n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y-NjGjlR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azbhg0qbzpj37hef5k7n.png" alt="The Soyuz spacecraft's control panel" width="880" height="433"&gt;&lt;/a&gt;&lt;em&gt;The Soyuz spacecraft's control panel. A thing of beauty. Mostly because of the knobs. (Stanislav Kozlovskiy, CC BY-SA 4.0, via Wikimedia Commons)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The secret lies in a little component called the &lt;em&gt;potentiometer&lt;/em&gt;. A potentiometer is a rotatable device whose output voltage is a fraction of its supply voltage. That fraction depends on how far you've rotated it. Aha! If we measure this output voltage, we'll know the fraction and we can use that to back-calculate how far the potentiometer/knob has been rotated. But how do we measure the output voltage from the potentiometer?&lt;/p&gt;

&lt;p&gt;In all digital electronics, voltage is measured using an &lt;em&gt;ADC&lt;/em&gt;, or &lt;em&gt;analog-to-digital converter&lt;/em&gt;. Voltage is considered 'analog' because it's a real-world physical quantity. It is the ADC's job is to measure that and convert it into a number that a microcontroller, such as our Raspberry Pi Pico, can understand. Luckily for us, the Raspberry Pi Pico comes with 3 ADC pins built-in! We can measure voltages to our hearts' delight. The 3 ADC pins are on the right side of the Pico as shown in the &lt;a href="https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf"&gt;pinout diagram&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4zDHnBuL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rris2fnfemnqicvgt4ig.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4zDHnBuL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rris2fnfemnqicvgt4ig.png" alt="The ADC pins are available on pin GP26, GP27 and GP28" width="880" height="290"&gt;&lt;/a&gt;&lt;em&gt;The ADC pins are available on pin GP26, GP27 and GP28&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Without further ado, let's get into it! Fire up a &lt;a href="https://wokwi.com/arduino/new?template=micropython-pi-pico"&gt;new MicroPython project&lt;/a&gt; in the Wokwi simulator, click the 'plus' button at the top of the simulator, and select the Potentiometer component. You'll notice that it has 3 pins: GND, VCC and SIG. These correspond to ground voltage, power supply voltage, and the signal voltage representing the amount of rotation, respectively.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SdzCr3v_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnusy6kwlm423vjaxfwc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SdzCr3v_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnusy6kwlm423vjaxfwc.gif" alt="GND, SIG and VCC pins on the potentiometer" width="600" height="366"&gt;&lt;/a&gt;&lt;em&gt;GND, SIG and VCC pins on the potentiometer&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's connect them up! Connect GND to any ground pin on the Pico - I chose pin 18 at the bottom-left. Connect VCC to the 3.3V output pin, which is pin 36 at the top-right. Lastly, wire up the SIG pin to one of the ADC pins - I connected mine to ADC0 which is at pin GP26 on the right. After wiring, your setup should look similar to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7ghm5YpW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0kx618cu0nqytdi4czt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7ghm5YpW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0kx618cu0nqytdi4czt.png" alt="Potentiometer connected up to the Pico in the simulator pane" width="880" height="825"&gt;&lt;/a&gt;&lt;em&gt;Potentiometer connected up to the Pico in the simulator pane&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Before we write the code, there's something you should know about ADCs. I mentioned earlier that it converts the voltage into "a number" for the microcontroller to use. However, that number is not the voltage itself. For example if the voltage is 2.2V, the ADC will not directly report "2.2" to the microcontroller. In the Pico's case, it will actually report the number "43690".&lt;/p&gt;

&lt;p&gt;Huh? Why 43690? Why is technology always so confusing? Fret not, there is an explanation. The ADC on the Pico is a &lt;em&gt;16-bit&lt;/em&gt; ADC, i.e. it has a numerical resolution of 16 bits. In simple terms, this means that it reports values from 0 at the minimum to (2^16 - 1), or 65535, at the maximum. An input voltage of 0V would correspond to an output value 0, and an input voltage of 3.3V would correspond to an output value of 65535. Any voltage in between corresponds linearly to a point between these two extremes. Let's visualize this with a graph.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yi9EiqtN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2gdb2zvid2e5yj54uvs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yi9EiqtN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2gdb2zvid2e5yj54uvs.png" alt="Plot of reported ADC value vs voltage on the Pi Pico's ADC" width="880" height="617"&gt;&lt;/a&gt;&lt;em&gt;Plot of reported ADC value vs voltage on the Pi Pico's ADC&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The relationship between the voltage seen by the ADC and the value it reports is linear. With that graph, it's clearer now why 2.2V corresponds to a reported value of 43690 on the Pico. If you would like to calculate that number instead of meticulously drawing a graph and finding the value by hand, there is a simple formula to convert voltage to ADC value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ADC value = Voltage * 65535 / 3.3&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we know the ADC reports 16-bit numbers to represent the voltage, we are ready to write some MicroPython and read the values from the potentiometer. We are going to use our trusty &lt;em&gt;machine&lt;/em&gt; and &lt;em&gt;time&lt;/em&gt; libraries to read the ADC value and to set the rate at which we read it. Type the code below into your Wokwi editor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n1r-u6O9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n6yztvp2gqndolwwxpw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n1r-u6O9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n6yztvp2gqndolwwxpw.png" alt="Code for reading the ADC on the Pico" width="602" height="408"&gt;&lt;/a&gt;&lt;em&gt;Code for reading the ADC on the Pico&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the first two lines we import the necessary libraries. We then set up pin GP26 as an ADC. Next we start the infinite loop where we read the 16-bit value from the ADC pin, print out this value in the console window, and sleep the Pico for 0.1 seconds. Let's run the simulation and play with the knob to see it in action! Press the 'play' button at the top of the simulator pane and use your mouse to rotate the potentiometer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jfaNjO-i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhx23cdg43tysqqqv9ja.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jfaNjO-i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhx23cdg43tysqqqv9ja.gif" alt="Turn it up to 11! I mean, 65535! Doesn't quite roll off the tongue 😐" width="600" height="427"&gt;&lt;/a&gt;&lt;em&gt;Turn it up to 11! I mean, 65535! Doesn't quite roll off the tongue 😐&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you were expecting that to be as satisfying as turning a real physical knob, I'm sorry to disappoint. Maybe this is how future generations will feel when turning knobs in their virtual reality metaverse.&lt;/p&gt;

&lt;p&gt;So far we've learned how to read the ADC, but we're not done yet. The real fun is in actually controlling things with the potentiometer. In our &lt;a href="https://bhave.sh/micropython-blink-led/"&gt;last tutorial&lt;/a&gt;, we wrote a program to blink the LED onboard the Pico every 0.5 seconds. But what if we want to change that number to make the LED blink faster or slower? We can use the potentiometer! The LED onboard the Pico is connected to pin &lt;em&gt;GP25&lt;/em&gt; - let's write a program to blink that LED, and use the knob to control its blink rate. This flowchart helps to visualize the program.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sdHKXIE4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4hapvntoap34l1c141i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sdHKXIE4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4hapvntoap34l1c141i.png" alt="Flowchart for controlling the LED blink rate using the potentiometer" width="861" height="936"&gt;&lt;/a&gt;&lt;em&gt;Flowchart for controlling the LED blink rate using the potentiometer&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We are almost ready to write the code. We're just missing a way to convert the ADC value to a time delay. We need to set a minimum and maximum time delay and map that to the minimum and maximum ADC values, 0 and 65535. To keep it very simple, let's have a minimum delay of 0.1 seconds and a maximum of 1.1 seconds. The formula for this is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Time delay, td = 0.1 + knob_value / 65535&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's modify our previous code to match the flowchart. The final code should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PYKSXNof--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jel09l1esdgdnrpr9ayv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PYKSXNof--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jel09l1esdgdnrpr9ayv.png" alt="Code for controlling the LED blink rate using the potentiometer" width="602" height="484"&gt;&lt;/a&gt;&lt;em&gt;Code for controlling the LED blink rate using the potentiometer&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Run your code by pressing the 'play' button in the simulator. Twist the knob and enjoy the incredible feeling of controlling our little green LED.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9_9o1Sw6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gccrwmm4p3rq08kg5t0c.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9_9o1Sw6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gccrwmm4p3rq08kg5t0c.gif" alt="Now if only I had to knob to control my heartbeat like that" width="600" height="410"&gt;&lt;/a&gt;&lt;em&gt;Now if only I had to knob to control my heartbeat like that&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That's it! Play with the code and see what interesting directions you can take this project in. As a small exercise, try reversing the logic so that the LED blinks &lt;em&gt;faster&lt;/em&gt; (instead of slower) when you turn the knob up. Good luck, and comment below or message me for any questions :)&lt;/p&gt;

&lt;p&gt;Next time in Secrets of MicroPython, we will use our new knowledge of ADCs to read data from a temperature sensor. Follow me to be notified when the next tutorial is out. Stay tuned!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This tutorial was originally published &lt;a href="https://bhave.sh/micropython-read-knob/"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>micropython</category>
      <category>python</category>
      <category>tutorial</category>
      <category>hardware</category>
    </item>
    <item>
      <title>Secrets of MicroPython: How to blink an LED</title>
      <dc:creator>Bhavesh Kakwani</dc:creator>
      <pubDate>Tue, 28 Dec 2021 20:44:20 +0000</pubDate>
      <link>https://dev.to/bhav/secrets-of-micropython-how-to-blink-an-led-cb6</link>
      <guid>https://dev.to/bhav/secrets-of-micropython-how-to-blink-an-led-cb6</guid>
      <description>&lt;p&gt;This tutorial was originally published &lt;a href="https://bhave.sh/micropython-blink-led/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Welcome to the Secrets of MicroPython tutorial series! MicroPython is an incredible tool for rapidly prototyping and iterating on hardware projects. It's similar to Arduino but instead of using C++, which you have to re-compile each time you make a code change, it runs on the Python interpreter which allows you to run code changes immediately. The quick feedback cycle enabled by MicroPython will, I believe, power the future of embedded development.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What is the "hello world" of embedded software? Why, blinking an LED, of course! In this first tutorial, we'll get you acquainted with the fundamentals of MicroPython and more importantly, the online simulator we'll be using for all the tutorials. That's right, an online simulator! No need to feel pressured to throw down money on a box of hardware gadgets to learn MicroPython - though you are certainly welcome to when you feel ready.&lt;/p&gt;

&lt;p&gt;In the simulator, we are going to be writing code for a Raspberry Pi Pico. The Pico is a powerful $4 microcontroller by the makers of the Raspberry Pi, and it comes with solid MicroPython support and documentation. The simulator we'll use is an excellent online service called Wokwi, and it has a &lt;a href="https://wokwi.com/arduino/new?template=micropython-pi-pico"&gt;starting point for Pi Pico + MicroPython projects here&lt;/a&gt;. I suggest you bookmark that link so you can always quickly get started on a new tutorial or project with one click. Here's a quick overview of the simulator window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1D0aL6q4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7yxc7wd8o1f2m3hw0j7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1D0aL6q4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7yxc7wd8o1f2m3hw0j7.png" alt="The Wokwi simulator" width="880" height="499"&gt;&lt;/a&gt;The Wokwi simulator. The left pane is where we write MicroPython code. The right pane is where we connect hardware to the Pico using wires. On the bottom right is the console which shows printed output from our program. Press the "play" button in the top right to see it in action!&lt;/p&gt;

&lt;p&gt;If you clicked the "play" button in the simulator, congratulations - you've just run your first MicroPython program! As shown in the screenshot above, it prints out "Hello, Pi Pico!" in the console on the bottom right. But we're here for something much more interesting - blinking LEDs (aw yeah)! If you look closely, there's a small LED on the top left corner of the Pico board, with the silkscreen "LED" printed in white underneath it. But which pin is it connected to?&lt;/p&gt;

&lt;p&gt;Luckily, we have Raspberry Pi Pico's excellent documentation to help us. You can snag the &lt;a href="https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf"&gt;pinout diagram here&lt;/a&gt;. Save it and maybe even print it out - it will be useful for every project. Let's zoom in and have a look at how to read the pinout diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XunhAJAV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvl0ecz89ggaaymy05l3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XunhAJAV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvl0ecz89ggaaymy05l3.png" alt="Top section of the Pi Pico pinout diagram" width="880" height="252"&gt;&lt;/a&gt;Top section of the Pi Pico pinout diagram. We can see from the label at the top that the onboard LED is connected to pin &lt;strong&gt;GP25&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So the onboard LED is on pin GP25. Take a moment here to also notice how pins 1 and 2 have &lt;em&gt;multiple&lt;/em&gt; labels attached to them. This is common in microcontrollers and it signifies that these pins can be used for any &lt;em&gt;one&lt;/em&gt; of these labeled functions, and that it's up to the programmer to decide which one will be active.&lt;/p&gt;

&lt;p&gt;In order to blink this happy little diode, we need to know two things: how to toggle the voltage on the output pin, and how to work with time in MicroPython. These are accomplished using the &lt;em&gt;machine&lt;/em&gt; and &lt;em&gt;time&lt;/em&gt; libraries, respectively. Before we write the code, it is helpful to visualize by drawing a flowchart.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DwPzCVmx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/je0a5ytj0pv3lzuniclc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DwPzCVmx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/je0a5ytj0pv3lzuniclc.png" alt="Flowchart for blinking an LED" width="839" height="1487"&gt;&lt;/a&gt;Flowchart for blinking an LED&lt;/p&gt;

&lt;p&gt;The code to accomplish this in MicroPython is fortunately quite simple. Take a look at the code below, and &lt;em&gt;type it in&lt;/em&gt; (&lt;strong&gt;do not copy-paste&lt;/strong&gt;) to your simulator. I can't stress enough that you should type it in yourself - it is the best way to truly understand programming and it's a very important habit to build in your learning, no matter how trivial a piece of code may seem. After you type it in, press the 'play' button to run the code and see your result!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XFE7QEZP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywijrweycpi2tewvxik4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XFE7QEZP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywijrweycpi2tewvxik4.png" alt="Code for blinking LED in MicroPython" width="552" height="382"&gt;&lt;/a&gt;Yes, I did just post the code as an image to prevent copy-pasting. Shame on you for trying. &lt;/p&gt;

&lt;p&gt;In the first two lines, we import the libraries we need. When using MicroPython with any kind of hardware, you will basically always need the &lt;em&gt;machine&lt;/em&gt; library as it provides you access to control every aspect of the hardware. The &lt;em&gt;time&lt;/em&gt; library is also needed in most projects so that we can control the rate at which things happen in our code. After importing the libraries, we set up the LED pin as a digital output. Finally we start an infinite loop in which the code sleeps (does nothing) for half a second, then toggles the LED pin. Check out the result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Kdc2fU6H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1govj18bb6zgu4j047g.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Kdc2fU6H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1govj18bb6zgu4j047g.gif" alt="Pi Pico onboard LED blinking" width="600" height="421"&gt;&lt;/a&gt; The simulated green photons emanating from this simulated LED are truly magnificent 🥲&lt;/p&gt;

&lt;p&gt;Cool! Are you satisfied? I am not. In real hardware projects, most of the LEDs to be controlled are &lt;em&gt;outside&lt;/em&gt; the microcontroller's circuit board. Let's take it up a notch and blink an external LED. When wiring up an LED, you need to ensure you are not putting too much current through it, otherwise it may 'pop' and die - trust me, I've done it too many times. To control the current going through it, we use a &lt;em&gt;resistor&lt;/em&gt;. The higher the resistance, the lower the current. The lower the current, the lower the brightness of the light from the LED.&lt;/p&gt;

&lt;p&gt;The Wokwi simulator doesn't actually display a different LED brightness based on the current, so for now we won't worry too much about the precise value of the resistor (in a real physical hardware project, you will need to calculate it based on your needs). We will just focus on how to wire it up. Let's first draw up a schematic diagram before implementing it in Wokwi.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hBdgTDs6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4kjqlwwbk2h60n03992.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hBdgTDs6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4kjqlwwbk2h60n03992.png" alt="Schematic for connecting external LED to Pi Pico" width="880" height="434"&gt;&lt;/a&gt;Current flows out from the Pico's output pin, through the LED and resistor, then to a ground pin on the Pico. &lt;/p&gt;

&lt;p&gt;In the right pane in Wokwi, click on the 'plus' button and add an LED into your design. Click the 'plus' button again and add a resistor. Let's connect them as shown in the schematic above. To connect, just click on your desired starting pin - e.g. the LED's negative pin / cathode - and then click on the destination pin - e.g. one of the pins on the resistor - to create a wire connection between those two points.&lt;/p&gt;

&lt;p&gt;Refer to the Pico's pinout diagram to figure out which pins you can use as 'output pin' and 'ground'. For the output pin, I will use pin GP0 (a.k.a pin 1 - confusing, I know) at the very top-left of the board. For the ground pin, I will connect to pin no. 3, also at the top-left. At this point you may be tempted to run the code, but first make sure to change the pin number in line no. 5 of the code from pin no. 25 to pin no. 0, so that we now blink the external LED instead of the onboard one. Click 'play' and watch the result!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--89mEwMpg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cn96tc38sibscjz3zadh.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--89mEwMpg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cn96tc38sibscjz3zadh.gif" alt="Blinking external LED using Pi Pico" width="600" height="238"&gt;&lt;/a&gt;Ahhh, the simulated red photons soothe my soul and do not remind me of an emergency vehicle speeding down the street and making very loud noises&lt;/p&gt;

&lt;p&gt;And there you have it! LEDs be blinking. As a small exercise/challenge, try to make both the onboard LED and the external LED blink in unison. And to take it up another notch, try to blink them perfectly out of sync, i.e. green is on when red is off, and vice versa. Good luck, and please ask any questions you have in the comments below.&lt;/p&gt;

&lt;p&gt;Next time in Secrets of MicroPython, we will learn to interface with and control other hardware peripherals and sensors. We will build our knowledge of all these parts before combining them to create cool projects! Stay tuned.&lt;/p&gt;

</description>
      <category>micropython</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
  </channel>
</rss>
