<?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: Przemysław Papla</title>
    <description>The latest articles on DEV Community by Przemysław Papla (@porok12).</description>
    <link>https://dev.to/porok12</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%2F1111913%2Ff9379047-d578-4958-8829-0575c33fb47c.png</url>
      <title>DEV Community: Przemysław Papla</title>
      <link>https://dev.to/porok12</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/porok12"/>
    <language>en</language>
    <item>
      <title>Raspberry Pico C: Remote Sensor</title>
      <dc:creator>Przemysław Papla</dc:creator>
      <pubDate>Sat, 30 Dec 2023 14:04:32 +0000</pubDate>
      <link>https://dev.to/porok12/raspberry-pico-c-remote-sensor-5b41</link>
      <guid>https://dev.to/porok12/raspberry-pico-c-remote-sensor-5b41</guid>
      <description>&lt;p&gt;Please, check out my &lt;a href="https://dev.to/blog/articles/1553849"&gt;previous article&lt;/a&gt; before continuing. As we will be relying on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article, we'll take on an interesting Raspberry Pi Pico project, aiming to turn it into an efficient temperature&lt;br&gt;
measurement device. I'll guide you through the basics of sensor interfacing and data acquisition, with a special focus&lt;br&gt;
on optimizing power usage. The unique aspect of this project involves periodically putting the Raspberry Pi Pico into a&lt;br&gt;
sleep state to conserve power. When it wakes up, it will make several measurements and transmit data to a remote server,&lt;br&gt;
repeating this cycle. Join me as we go step by step, emphasizing the importance of power efficiency in our endeavor.&lt;/p&gt;
&lt;h2&gt;
  
  
  Raspberry Pico
&lt;/h2&gt;

&lt;p&gt;To achieve our goals, we need a &lt;code&gt;Raspberry Pico W&lt;/code&gt;. The "W" in Raspberry Pico products indicates the inclusion of&lt;br&gt;
wireless features such as WiFi and Bluetooth.&lt;/p&gt;

&lt;p&gt;Connect the DHT22 sensor to your Raspberry Pico. You can find the DHT22&lt;br&gt;
datasheet &lt;a href="https://www.sparkfun.com/datasheets/Sensors/Temperature/DHT22.pdf" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Ensure that you have the required&lt;br&gt;
resistor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdhqoqqstc%2Fimage%2Fupload%2Fv1703532283%2Fblog%2Fblinking_led%2Fx7kfwpczxohmyodzbrjr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdhqoqqstc%2Fimage%2Fupload%2Fv1703532283%2Fblog%2Fblinking_led%2Fx7kfwpczxohmyodzbrjr.jpg" alt="Schema"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: It can be any sensor, I will use DHT22 for demonstration purposes.&lt;/p&gt;
&lt;h2&gt;
  
  
  Project setup
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Development environment
&lt;/h3&gt;

&lt;p&gt;Before you begin, make sure to set up the necessary environment variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;PICO_BOARD&lt;/code&gt;: &lt;code&gt;pico_w&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PICO_SDK_PATH&lt;/code&gt;: &lt;code&gt;&amp;lt;your-path&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Raspberry Pico will connect to a server via a TCP connection, so we need to set the following CMake options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;TCP_SERVER_IP&lt;/code&gt;: Address of the server that will listen to Raspberry Pico eg. &lt;code&gt;192.168.1.XX&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TCP_PORT&lt;/code&gt;: Port on the server that is open for communication eg. &lt;code&gt;8008&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WIFI_SSID&lt;/code&gt;: Your WiFi network's SSID&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WIFI_PASSWORD&lt;/code&gt;: Your WiFi network's password&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: Raspberry must connect to network where such host server exists.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdhqoqqstc%2Fimage%2Fupload%2Fv1703509236%2Fblog%2Fblinking_led%2Fkimuz623yyjpcfh1djnn.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdhqoqqstc%2Fimage%2Fupload%2Fv1703509236%2Fblog%2Fblinking_led%2Fkimuz623yyjpcfh1djnn.webp" alt="Settings"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Prepare project
&lt;/h3&gt;

&lt;p&gt;Copying the files &lt;code&gt;lwipopts.h&lt;/code&gt;, &lt;code&gt;lwipopts_examples_common.h&lt;/code&gt; and &lt;code&gt;pico_extras_import.cmake&lt;/code&gt;, &lt;code&gt;pico_sdk_import.cmake&lt;/code&gt; is&lt;br&gt;
essential for configuring and importing necessary settings and dependencies. Here's a&lt;br&gt;
breakdown of each file:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;lwipopts.h&lt;/code&gt; and &lt;code&gt;lwipopts_examples_common.h&lt;/code&gt;:&lt;br&gt;
These files contain configuration options for the Lightweight IP (lwIP) stack, which is crucial for networking&lt;br&gt;
functionality.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;pico_extras_import.cmake&lt;/code&gt; and &lt;code&gt;pico_sdk_import.cmake&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These CMake scripts are necessary for importing the Pico SDK and associated extras into your project.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pico_sdk_import.cmake&lt;/code&gt; imports the core Pico SDK, while &lt;code&gt;pico_extras_import.cmake&lt;/code&gt; imports additional extras for
specific functionalities.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By copying these files, you set up the foundational configurations and dependencies needed for networking and general&lt;br&gt;
development on the Raspberry Pico platform.&lt;/p&gt;

&lt;p&gt;You can find them in the Pico SDK repository. Follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;lwipopts.h&lt;/code&gt; and &lt;code&gt;lwipopts_examples_common.h&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to the &lt;a href="https://github.com/raspberrypi/pico-examples/tree/master/pico_w/wifi" rel="noopener noreferrer"&gt;Pico Examples repository&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Inside the repository, locate the &lt;code&gt;pico_w/wifi&lt;/code&gt; directory.&lt;/li&gt;
&lt;li&gt;Copy the &lt;code&gt;wifi/lwipopts.h&lt;/code&gt; and &lt;code&gt;lwipopts_examples_common.h&lt;/code&gt; files from this directory to your project.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;pico_sdk_import.cmake&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;a href="https://github.com/raspberrypi/pico-sdk/tree/master/external" rel="noopener noreferrer"&gt;Pico SDK repository&lt;/a&gt;, find
the &lt;code&gt;external/pico_sdk_import.cmake&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;Copy this file into your project directory.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;pico_extras_import.cmake&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;a href="https://github.com/raspberrypi/pico-extras/tree/master/external" rel="noopener noreferrer"&gt;Pico Extras repository&lt;/a&gt;, find
the &lt;code&gt;external/pico_extras_import.cmake&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;Copy this file into your project directory.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Writing code
&lt;/h3&gt;

&lt;p&gt;The code is available on&lt;br&gt;
my &lt;a href="https://github.com/Porok12/blog-examples/tree/master/raspberry-pico-c-remote-sensor" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Cmake
&lt;/h4&gt;

&lt;p&gt;Let's dive into the code, starting with the essential CMake configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;cmake_minimum_required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;VERSION 3.13&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;pico_sdk_import.cmake&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;pico_extras_import.cmake&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;project&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_remote_sensor C CXX ASM&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;CMAKE_C_STANDARD 11&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;CMAKE_CXX_STANDARD 17&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;WIFI_SSID &lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;WIFI_SSID&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; CACHE INTERNAL &lt;span class="s2"&gt;"WiFi SSID"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;WIFI_PASSWORD &lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;WIFI_PASSWORD&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; CACHE INTERNAL &lt;span class="s2"&gt;"WiFi password"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;TCP_SERVER_IP &lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;TCP_SERVER_IP&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; CACHE INTERNAL &lt;span class="s2"&gt;"TCP server ip"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;TCP_PORT &lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;TCP_PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; CACHE INTERNAL &lt;span class="s2"&gt;"TCP server port"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Raspberry Pi Pico SDK version &lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;PICO_SDK_VERSION_STRING&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;pico_sdk_init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nb"&gt;add_executable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_remote_sensor main.c src/dht.c src/tcp_utils.c src/sleep_utils.c&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;target_include_directories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_remote_sensor PRIVATE &lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;CMAKE_CURRENT_LIST_DIR&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;target_link_libraries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_remote_sensor
        pico_cyw43_arch_lwip_poll &lt;span class="c1"&gt;# pico_cyw43_arch_lwip_threadsafe_background&lt;/span&gt;
        pico_stdlib
        hardware_gpio
        hardware_sleep
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;target_compile_definitions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_remote_sensor PRIVATE
        WIFI_SSID=\"&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;WIFI_SSID&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;\"
        WIFI_PASSWORD=\"&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;WIFI_PASSWORD&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;\"
        TCP_SERVER_IP=\"&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;TCP_SERVER_IP&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;\"
        TCP_PORT=\"&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;TCP_PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;\"
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;pico_enable_stdio_usb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_remote_sensor 1&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;pico_enable_stdio_uart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_remote_sensor 0&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;pico_add_extra_outputs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_remote_sensor&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a previous article, we provided a detailed description of a similar file. Here, you'll find a comparable one, with&lt;br&gt;
added environment variables&lt;br&gt;
and &lt;a href="https://github.com/raspberrypi/pico-extras" rel="noopener noreferrer"&gt;Pico Extras&lt;/a&gt; that adds &lt;code&gt;pico_sleep&lt;/code&gt; functionality.&lt;/p&gt;
&lt;h4&gt;
  
  
  Main
&lt;/h4&gt;

&lt;p&gt;Now, let's move on to the main C file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"pico/stdlib.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"pico/cyw43_arch.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"src/sleep_utils.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"src/tcp_utils.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"src/dht.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have included:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;stdlib.h&amp;gt;&lt;/code&gt;: Standard C library for general utilities&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"pico/stdlib.h"&lt;/code&gt;: Pico SDK standard library header for Raspberry Pi Pico&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"pico/cyw43_arch.h"&lt;/code&gt;: Header for the Wi-Fi chip used with the Raspberry Pi Pico&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"src/...""&lt;/code&gt;: Our files, which we will implement in a moment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will also need some utils methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Additional PIN for debugging
 */&lt;/span&gt;
&lt;span class="cp"&gt;#define LED_PIN 22
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;init_led&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;gpio_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;gpio_set_dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GPIO_OUT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;init_wifi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cyw43_arch_init_with_country&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CYW43_COUNTRY_POLAND&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Wi-Fi failed to initialise&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;cyw43_arch_enable_sta_mode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Connecting to Wi-Fi...&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cyw43_arch_wifi_connect_timeout_ms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;WIFI_SSID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;WIFI_PASSWORD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CYW43_AUTH_WPA2_AES_PSK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to connect.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Connected.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a brief overview of the &lt;strong&gt;Wi-Fi Initialization Function&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;int init_wifi(void) { /*...*/ }&lt;/code&gt;: This function initializes the Wi-Fi module using the Cypress CYW4343X chip.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cyw43_arch_init_with_country(CYW43_COUNTRY_POLAND)&lt;/code&gt;: Initializes the Wi-Fi module with the specified country
code (Poland in this case).&lt;/li&gt;
&lt;li&gt;If initialization fails, an error message is printed, and the function returns 1.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cyw43_arch_enable_sta_mode()&lt;/code&gt;: Enables the Wi-Fi station mode.&lt;/li&gt;
&lt;li&gt;The function attempts to connect to the Wi-Fi network using the provided SSID, password, and encryption type (
WPA2 in this case) with a timeout of 10 seconds.&lt;/li&gt;
&lt;li&gt;If the connection fails, an error message is printed, and the function returns 1.&lt;/li&gt;
&lt;li&gt;If the connection is successful, a success message is printed, and the function returns 0.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;comparator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)(((&lt;/span&gt;&lt;span class="n"&gt;dht_reading&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;temp_celsius&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;dht_reading&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;temp_celsius&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The provided C function, named &lt;code&gt;comparator&lt;/code&gt;, is designed for comparing two objects of type &lt;code&gt;dht_reading&lt;/code&gt; based on their&lt;br&gt;
temperature values in Celsius. This function will be used as a callback function for sorting an array of &lt;code&gt;dht_reading&lt;/code&gt;&lt;br&gt;
objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;volatile&lt;/span&gt; &lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;awake&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sleep_callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;awake&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* Initialize */&lt;/span&gt;

    &lt;span class="n"&gt;stdio_init_all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;init_led&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;dht_init&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="cm"&gt;/* Definitions */&lt;/span&gt;

    &lt;span class="n"&gt;dht_reading&lt;/span&gt; &lt;span class="n"&gt;measurements&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;scb_orig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scb_hw&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;scr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;clock0_orig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;clocks_hw&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;sleep_en0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;clock1_orig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;clocks_hw&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;sleep_en1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;datetime_t&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2020&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;month&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mo"&gt;06&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mo"&gt;05&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dotw&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 0 is Sunday, so 5 is Friday&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hour&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mo"&gt;00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sec&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mo"&gt;00&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;rtc_init&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="cm"&gt;/* Logic */&lt;/span&gt;

    &lt;span class="n"&gt;sleep_ms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;measure_freqs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;sleep_ms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;awake&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;gpio_put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cyw43_is_initialized&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;cyw43_state&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;cyw43_arch_deinit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;sleep_run_from_xosc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// UART will be reconfigured by sleep_run_from_xosc&lt;/span&gt;
        &lt;span class="n"&gt;rtc_set_datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;rtc_sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sleep_callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;awake&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Should be sleeping&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;gpio_put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;recover_from_sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scb_orig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;clock0_orig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;clock1_orig&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;init_wifi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;dht_reading&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;read_from_dht&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%f %f&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;temp_celsius&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;measurements&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;sleep_ms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;qsort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;measurements&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dht_reading&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;comparator&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;send_measurements&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;measurements&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The provided C code represents the main logic for the project involving sleep cycles, sensor measurements, and wireless&lt;br&gt;
communication. Here's a breakdown of the code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Global Variables&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;static volatile bool awake;&lt;/code&gt;: Declares a global boolean variable to track the wake state of the system.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Sleep Callback Function&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;static void sleep_callback&lt;/code&gt;: Defines a callback function triggered when the system wakes from sleep. It sets the
global &lt;code&gt;awake&lt;/code&gt; variable to &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Initialization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;stdio_init_all()&lt;/code&gt;: Initializes standard I/O for printf.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;init_led()&lt;/code&gt;: Initializes an LED PIN.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dht_init()&lt;/code&gt;: Initializes the DHT sensor.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Definitions&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defines an array &lt;code&gt;dht_reading measurements[5]&lt;/code&gt; for storing sensor readings.&lt;/li&gt;
&lt;li&gt;Initializes variables for storing original values of certain hardware registers.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Date and Time Configuration&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defines a &lt;code&gt;datetime_t&lt;/code&gt; structure with a specific date and time.&lt;/li&gt;
&lt;li&gt;Initializes the RTC (Real-Time Clock) with the provided date and time.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Main Loop&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initiates a loop for continuous operation.&lt;/li&gt;
&lt;li&gt;Sets &lt;code&gt;awake&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt; and turns off the LED.&lt;/li&gt;
&lt;li&gt;Checks if the Wi-Fi module is initialized and deinitializes it if necessary.&lt;/li&gt;
&lt;li&gt;Initiates a sleep cycle using &lt;code&gt;rtc_sleep()&lt;/code&gt; and waits for the sleep callback to set &lt;code&gt;awake&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Prints a message during the sleep cycle.&lt;/li&gt;
&lt;li&gt;Turns on the LED and recovers from sleep.&lt;/li&gt;
&lt;li&gt;Initializes the Wi-Fi module.&lt;/li&gt;
&lt;li&gt;Reads sensor data from the DHT sensor, sorts the measurements, and sends the data wirelessly.&lt;/li&gt;
&lt;li&gt;Repeats the loop.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Server
&lt;/h2&gt;

&lt;p&gt;This Python server script serves as a TCP server for Raspberry Pi Pico. It listens for incoming connections, receives&lt;br&gt;
temperature and humidity data in a specific format from the Pico device. The server runs indefinitely, continuously&lt;br&gt;
accepting and processing incoming data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# imports
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;argparse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Namespace&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;buffer_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
    &lt;span class="n"&gt;payload_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;  &lt;span class="c1"&gt;# Specific to what PICO sends
&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Listening for client...&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connected by &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer_size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;payload_size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                            &lt;span class="n"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ff&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Temp: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, Humidity: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;humidity&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                                &lt;span class="k"&gt;break&lt;/span&gt;
                        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                            &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                            &lt;span class="k"&gt;break&lt;/span&gt;
                &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;KeyboardInterrupt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Exiting...&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key functionalities include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Database Storage&lt;/strong&gt;: The server can save the received temperature and humidity measurements to a local SQLite
database if configured.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continuous Operation&lt;/strong&gt;: The script runs continuously, maintaining an open connection for data reception.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connection Handling&lt;/strong&gt;: The server handles incoming connections, receiving and processing data in the expected
format.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Graceful Exit&lt;/strong&gt;: The script can be interrupted with a keyboard interrupt (Ctrl+C), allowing for a graceful exit.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-p&lt;/code&gt; or &lt;code&gt;--port&lt;/code&gt;: Specifies the port on which the server listens (default is 8008).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--ip&lt;/code&gt;: Specifies the IP address on which the server operates (default is an empty string, allowing connections from
any IP).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--db&lt;/code&gt;: Specifies the location of the SQLite database for storing temperature and humidity data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To run the server, execute the script and provide any necessary configuration options.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Server Setup&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Begin by launching the Python server script on the designated host machine. Ensure that the server is running and
ready to accept connections.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Raspberry Pico Flashing&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flash the Raspberry Pico with the programmed firmware using the appropriate development environment or tool.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Observation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Observe the behavior and interactions between the Raspberry Pico and the server.&lt;/li&gt;
&lt;li&gt;Verify that temperature and humidity data is successfully transmitted from the Pico to the server.&lt;/li&gt;
&lt;li&gt;Confirm any expected actions or responses, such as database storage on the server.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Enjoy the Results&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once the testing process is complete and the interactions are confirmed, you can consider the setup successful.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdhqoqqstc%2Fimage%2Fupload%2Fv1703521376%2Fblog%2Fblinking_led%2Fu1lx7uhl3c7rhlplvk0q.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdhqoqqstc%2Fimage%2Fupload%2Fv1703521376%2Fblog%2Fblinking_led%2Fu1lx7uhl3c7rhlplvk0q.webp" alt="Testing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In summary, this article provides a comprehensive guide for creating a temperature measurement system using the&lt;br&gt;
Raspberry Pi Pico. Starting from the basics of sensor interfacing and data acquisition, we progress through the&lt;br&gt;
essential steps of configuring the Pico to act as a robust temperature monitor. We extend our exploration to seamlessly&lt;br&gt;
forward this valuable temperature data to a remote server, adding a layer of connectivity to our Pico project. At the&lt;br&gt;
end, you have a smart temperature monitoring solution, offering practical insights for real-world applications.&lt;/p&gt;

</description>
      <category>iot</category>
      <category>c</category>
      <category>raspberrypi</category>
      <category>embedded</category>
    </item>
    <item>
      <title>RaspberryPi Pico with C: Blinking LED</title>
      <dc:creator>Przemysław Papla</dc:creator>
      <pubDate>Sat, 05 Aug 2023 19:40:07 +0000</pubDate>
      <link>https://dev.to/porok12/wip-raspberrypi-pico-with-c-blinking-led-4e70</link>
      <guid>https://dev.to/porok12/wip-raspberrypi-pico-with-c-blinking-led-4e70</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article, we'll explore embedded programming and show you how to make a blinking LED on the Raspberry Pico&lt;br&gt;
using C language. Whether you're a beginner or have some experience, we'll go through each step to help you get the most&lt;br&gt;
out of your Raspberry Pico.&lt;/p&gt;

&lt;p&gt;We will use CMake. CMake is a open-source cross-platform tool that simplifies the process of building and managing&lt;br&gt;
software projects. It helps developers create consistent and platform-independent build configurations, making it easier&lt;br&gt;
to work on different systems.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Preparations
&lt;/h3&gt;

&lt;p&gt;To ensure a comprehensive working environment, install the necessary libraries. On Ubuntu you can run the following&lt;br&gt;
command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition to this we will need &lt;code&gt;pico_sdk_import.cmake&lt;/code&gt; file. In order to get this we copy it&lt;br&gt;
from &lt;code&gt;$PICO_SDK_PATH/external&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/raspberrypi/pico-sdk.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Development environment
&lt;/h3&gt;

&lt;p&gt;We'll be using CLion by JetBrains as our IDE for this project. CLion streamlines coding with features like intelligent&lt;br&gt;
code completion and robust debugging tools, making our development process more efficient.&lt;/p&gt;

&lt;p&gt;Remember to set essential evironment variables like &lt;code&gt;PICO_BOARD&lt;/code&gt; or &lt;code&gt;PICO_SK_PATH&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g4m2Fl6I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/dhqoqqstc/image/upload/v1703505672/blog/blinking_led/shvjrasqrnsztn83kr1k.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g4m2Fl6I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/dhqoqqstc/image/upload/v1703505672/blog/blinking_led/shvjrasqrnsztn83kr1k.webp" alt="Settings" width="800" height="526"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's code it
&lt;/h2&gt;

&lt;p&gt;To begin, create or update the CMakeLists.txt file with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;cmake_minimum_required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;VERSION 3.26&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;pico_sdk_import.cmake&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;project&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_blinking_led C CXX ASM&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;pico_sdk_init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nb"&gt;add_executable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_blinking_led main.c&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;target_link_libraries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_blinking_led pico_stdlib&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;pico_add_extra_outputs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;raspberry_pico_c_blinking_led&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a breakdown of the key elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;include(pico_sdk_import.cmake)&lt;/code&gt;: This line imports the pico_sdk, an essential component for Raspberry Pico
development.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;project(raspberry_pico_c_blinking_led C CXX ASM)&lt;/code&gt;: Declares the project, specifying support for C, C++, and Assembly
languages.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pico_sdk_init()&lt;/code&gt;: Initializes the pico_sdk for the project.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;add_executable(raspberry_pico_c_blinking_led main.c)&lt;/code&gt;: Defines the executable and points to the main source file (
main.c).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;target_link_libraries(raspberry_pico_c_blinking_led pico_stdlib)&lt;/code&gt;: Links the pico_stdlib library to the executable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pico_add_extra_outputs(raspberry_pico_c_blinking_led)&lt;/code&gt;: Adds extra outputs for the project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's move on to the &lt;code&gt;main.c&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"pico/stdlib.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;gpio_toggle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;pin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;gpio_put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;gpio_get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pin&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="cp"&gt;#ifndef PICO_DEFAULT_LED_PIN
#warning blink example requires a board with a regular LED
#else
&lt;/span&gt;    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;LED_PIN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PICO_DEFAULT_LED_PIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;gpio_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;gpio_set_dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GPIO_OUT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;gpio_toggle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED_PIN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;sleep_ms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cp"&gt;#endif
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a breakdown of the code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Include Statement: The code includes the necessary header file &lt;code&gt;pico/stdlib.h&lt;/code&gt; which is part of the Pico SDK.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;gpio_toggle&lt;/code&gt; Function:

&lt;ul&gt;
&lt;li&gt;This function takes a GPIO pin as a parameter.&lt;/li&gt;
&lt;li&gt;It toggles the state of the specified GPIO pin (switches it from high to low and vice versa).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;main&lt;/code&gt; Function:

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;#ifndef PICO_DEFAULT_LED_PIN&lt;/code&gt; block, checks if the default LED pin is defined for the board. If not, it emits
a compilation warning.&lt;/li&gt;
&lt;li&gt;Inside the &lt;code&gt;#else&lt;/code&gt; block (indicating that a regular LED pin is available):

&lt;ul&gt;
&lt;li&gt;The LED pin is set as &lt;code&gt;PICO_DEFAULT_LED_PIN&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;GPIO initialization and configuration are performed to set the LED pin as an output.&lt;/li&gt;
&lt;li&gt;The main loop toggles the LED state and sleeps for 250 milliseconds, creating a blinking effect.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code is available on&lt;br&gt;
my &lt;a href="https://github.com/Porok12/blog-examples/tree/master/raspberry-pico-c-blinking-led"&gt;GitHub repository&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Flashing Raspberry Pico
&lt;/h2&gt;

&lt;p&gt;To begin the update process, reload your CMake project in CLion. Once reloaded, proceed by clicking the 'Build' button.&lt;br&gt;
This action initiates the compilation process, resulting in the creation of a &lt;code&gt;.uf2&lt;/code&gt; file. You can find this file within&lt;br&gt;
the &lt;code&gt;build&lt;/code&gt; folder of your project directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L47bsjBk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/dhqoqqstc/image/upload/v1703506401/blog/blinking_led/f4yjovcecpp3lsp4swq5.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L47bsjBk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/dhqoqqstc/image/upload/v1703506401/blog/blinking_led/f4yjovcecpp3lsp4swq5.webp" alt="Build" width="800" height="651"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To flash your raspberry follow steps from &lt;a href="https://dev.to/blog/articles/1552276#flashing"&gt;the previous article&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;I've guided through the process of implementing a blinking LED on the Raspberry Pico using the C programming language.&lt;br&gt;
We set up the development environment within CLion, installed necessary libraries, and coded the LED blinking&lt;br&gt;
functionality. To conclude, we've learned the flashing procedure, creating a &lt;code&gt;.uf2&lt;/code&gt; file for the Raspberry Pico by&lt;br&gt;
reloading the CMake project and initiating the &lt;code&gt;Build&lt;/code&gt; operation.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.raspberrypi.com/documentation/microcontrollers/c_sdk.html"&gt;Raspberry Pico C/C++ SDK Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>c</category>
      <category>raspberrypi</category>
      <category>embedded</category>
      <category>iot</category>
    </item>
    <item>
      <title>RaspberryPI Pico with Python: Make Firmware</title>
      <dc:creator>Przemysław Papla</dc:creator>
      <pubDate>Sun, 30 Jul 2023 07:32:37 +0000</pubDate>
      <link>https://dev.to/porok12/wip-raspberrypi-pico-with-python-make-firmware-18pd</link>
      <guid>https://dev.to/porok12/wip-raspberrypi-pico-with-python-make-firmware-18pd</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article, I'll walk you through the process of crafting a custom firmware file for the Raspberry Pi Pico using&lt;br&gt;
MicroPython. We'll explore the simplicity and power of MicroPython, providing step-by-step&lt;br&gt;
instructions for creating a personalized firmware. By the end, you'll have the skills to shape the capabilities of your&lt;br&gt;
Raspberry Pi Pico, enhancing its versatility and tailoring it to your specific project needs. Let's delve into the world&lt;br&gt;
of MicroPython and enhance your microcontroller programming experience!&lt;/p&gt;
&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Clone micropython
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Choose Location:

&lt;ul&gt;
&lt;li&gt;Decide on a suitable location on your PC where you want to store the MicroPython repository.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Open Terminal:

&lt;ul&gt;
&lt;li&gt;Navigate to the location where you want to clone the repository. You can use
the &lt;code&gt;cd&lt;/code&gt; command to change directories.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Clone the Repository:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run the following command to clone the MicroPython repository into the chosen location:
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  git clone git@github.com:micropython/micropython.git
  &lt;span class="nb"&gt;cd &lt;/span&gt;micropython
  git submodule update &lt;span class="nt"&gt;--init&lt;/span&gt; lib/pico-sdk lib/tinyusb
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once these steps are completed, you'll have successfully cloned the MicroPython repository to your local machine, and&lt;br&gt;
you can proceed with the next steps in your project.&lt;/p&gt;
&lt;h3&gt;
  
  
  Create module
&lt;/h3&gt;

&lt;p&gt;Create a new file named &lt;code&gt;main.py&lt;/code&gt; within the &lt;code&gt;$MICROPYTHON_PATH/ports/rp2/modules&lt;/code&gt; directory. This is a file in which&lt;br&gt;
you can write your own script. As example, I will use the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;machine&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Pin&lt;/span&gt;

&lt;span class="n"&gt;led&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Pin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OUT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;led&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;led&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create firmware
&lt;/h2&gt;

&lt;p&gt;This sequence of commands is used to build and compile our firmware.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;micropython/mpy-cross&lt;span class="nv"&gt;$ &lt;/span&gt;make
micropython/ports/rp2&lt;span class="nv"&gt;$ &lt;/span&gt;make submodules
micropython/ports/rp2&lt;span class="nv"&gt;$ &lt;/span&gt;make
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;make&lt;/code&gt;: This command is executed in the &lt;code&gt;micropython/mpy-cross directory&lt;/code&gt;. It invokes the make tool to build the
MicroPython cross-compiler.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;make submodules&lt;/code&gt;: Executed in the &lt;code&gt;micropython/ports/rp2 directory&lt;/code&gt;, this command initializes and updates the
submodules required for the Raspberry Pico.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;make&lt;/code&gt;: The final command, also executed in the &lt;code&gt;micropython/ports/rp2 directory&lt;/code&gt;, triggers the actual build process
for MicroPython on the Raspberry
Pi Pico. It compiles the code and generates the firmware that can be flashed onto the microcontroller&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Upon executing the specified commands, a &lt;code&gt;uf2&lt;/code&gt; file will be generated as the output. This &lt;code&gt;uf2&lt;/code&gt; file encapsulates the&lt;br&gt;
compiled and processed MicroPython firmware, ready to be flashed onto the Raspberry Pico or the target device&lt;br&gt;
specified in the build configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls &lt;/span&gt;build-PICO/&lt;span class="k"&gt;*&lt;/span&gt;.uf2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To flash your raspberry follow steps from &lt;a href="https://dev.to/blog/articles/1552276#flashing"&gt;the previous article&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In short, this article helps you make a custom firmware for the Raspberry Pico using MicroPython. It explains setup&lt;br&gt;
steps, like cloning MicroPython and creating a module with a sample LED blink script. The step-by-step guide then shows&lt;br&gt;
how to create a uf2 file ready to flash onto the Raspberry Pico.&lt;/p&gt;

&lt;p&gt;The complete code is available on&lt;br&gt;
my &lt;a href="https://github.com/Porok12/blog-examples/tree/master/raspberry-pico-python-blinking-led"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.raspberrypi.com/documentation/microcontrollers/micropython.html"&gt;MicroPython&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>raspberrypi</category>
      <category>embedded</category>
      <category>iot</category>
    </item>
    <item>
      <title>RaspberryPI Pico with Python: Blinking LED</title>
      <dc:creator>Przemysław Papla</dc:creator>
      <pubDate>Fri, 28 Jul 2023 16:32:23 +0000</pubDate>
      <link>https://dev.to/porok12/wip-raspberry-pico-blinking-led-2npo</link>
      <guid>https://dev.to/porok12/wip-raspberry-pico-blinking-led-2npo</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The Raspberry Pico, developed by the Raspberry Pi Foundation, is a specialized microcontroller board designed for a&lt;br&gt;
variety of applications. Unlike traditional Raspberry Pi single-board computers, it caters specifically to the needs of&lt;br&gt;
microcontroller projects, providing a compact and versatile platform for both beginners and experienced developers.&lt;/p&gt;

&lt;p&gt;Key features of the Raspberry Pico include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;RP2040 MCU:&lt;/strong&gt; Pico features a dual-core ARM Cortex-M0+ for balanced performance and power efficiency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;26 GPIO Pins:&lt;/strong&gt; Equipped for easy connection and control of external devices and sensors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Versatile Peripherals:&lt;/strong&gt; Supports SPI, I2C, UART, PWM, and more for diverse hardware interfacing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-Language Support:&lt;/strong&gt; Pico is programmable in MicroPython and for C language.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cost-Effective, Compact:&lt;/strong&gt; Budget-friendly and designed for space-constrained embedded projects.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Raspberry Pico is commonly used in projects that require the functionality of a microcontroller, such as&lt;br&gt;
robotics, embedded systems, and various DIY electronics projects. It provides an affordable and accessible platform for&lt;br&gt;
learning and experimenting with physical computing.&lt;/p&gt;
&lt;h2&gt;
  
  
  Development Environment
&lt;/h2&gt;

&lt;p&gt;We will utilize &lt;code&gt;PyCharm&lt;/code&gt; as our preferred IDE (integrated development environment) for&lt;br&gt;
programming the Raspberry Pico. &lt;code&gt;PyCharm&lt;/code&gt; provides a user-friendly interface, powerful code editing features, and&lt;br&gt;
seamless integration with Python development.&lt;/p&gt;

&lt;p&gt;In this article we will be programming using the MicroPython, so you will need to install the MicroPython plugin. It can&lt;br&gt;
be downloaded from the following &lt;a href="https://plugins.jetbrains.com/plugin/9777-micropython"&gt;page&lt;/a&gt;. If you're using a&lt;br&gt;
different IDE, be sure to explore and install the appropriate plugins accordingly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Caea2Fe---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/dhqoqqstc/image/upload/v1690661594/blog/blinking_led/rzg4u0nqksung1azitw2.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Caea2Fe---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/dhqoqqstc/image/upload/v1690661594/blog/blinking_led/rzg4u0nqksung1azitw2.webp" alt="PyCharm" width="800" height="580"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Write the code
&lt;/h2&gt;

&lt;p&gt;Create a new &lt;code&gt;PyCharm&lt;/code&gt; project and create the &lt;code&gt;main.py&lt;/code&gt; file. Then paste the following snippet. This code is a simple&lt;br&gt;
example of controlling an LED.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;machine&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Pin&lt;/span&gt;

&lt;span class="n"&gt;led&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Pin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OUT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;led&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;led&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down the code step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The code begins by importing two modules:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;time&lt;/code&gt; for managing time-related functions,&lt;/li&gt;
&lt;li&gt;and &lt;code&gt;Pin&lt;/code&gt; from the &lt;code&gt;machine&lt;/code&gt; module, which is commonly used in MicroPython for GPIO (General-Purpose Input/Output)
operations.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Initializing LED Pin

&lt;ul&gt;
&lt;li&gt;It creates an instance of the &lt;code&gt;Pin&lt;/code&gt; class named &lt;code&gt;led&lt;/code&gt;. This pin is configured as an output (&lt;code&gt;Pin.OUT&lt;/code&gt;) and is
connected to physical pin 25 on the microcontroller or board.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;LED Blinking Loop:

&lt;ul&gt;
&lt;li&gt;The code enters an infinite loop (&lt;code&gt;while True&lt;/code&gt;) to continuously toggle the LED on and off.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;led.value(1)&lt;/code&gt; sets the LED pin to a high state (turning the LED on).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;time.sleep(0.5)&lt;/code&gt; pauses the program for 0.5 seconds, creating a half-second delay.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;led.value(0)&lt;/code&gt; sets the LED pin to a low state (turning the LED off)&lt;/li&gt;
&lt;li&gt;Another &lt;code&gt;time.sleep(0.5)&lt;/code&gt; introduces another half-second delay before the loop repeats.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This code creates a simple blinking pattern for an LED connected to pin 25. The LED turns on for 0.5 seconds, turns off&lt;br&gt;
for 0.5 seconds, and the cycle repeats indefinitely.&lt;/p&gt;

&lt;p&gt;In Raspberry Pico, pin 25 is connected with builtin green LED (For more details&lt;br&gt;
see &lt;a href="https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html"&gt;pinout schemas&lt;/a&gt;)&lt;/p&gt;
&lt;h2&gt;
  
  
  Flashing
&lt;/h2&gt;

&lt;p&gt;Flashing the Raspberry Pico involves updating the firmware on the microcontroller.&lt;/p&gt;
&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Raspberry Pico: Ensure you have the Raspberry Pico microcontroller board.&lt;/li&gt;
&lt;li&gt;Micro USB Cable: Use a micro USB cable to connect the Pico to your computer.&lt;/li&gt;
&lt;li&gt;Get the right MicroPython UF2 file by downloading it from
the &lt;a href="https://micropython.org/download/?vendor=Raspberry%20Pi"&gt;official source&lt;/a&gt;, such as
the &lt;a href="https://micropython.org/download/rp2-pico/rp2-pico-latest.uf2"&gt;latest Raspberry Pico file&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Flash Raspberry Pico with MicroPython
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Enter Bootloader Mode:

&lt;ul&gt;
&lt;li&gt;Ensure the Raspberry Pico is not connected to power.&lt;/li&gt;
&lt;li&gt;Hold down the BOOTSEL button on the Pico.&lt;/li&gt;
&lt;li&gt;While holding the button, connect the Pico to your computer using the micro USB cable.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Locate Pico as a Mass Storage Device:

&lt;ul&gt;
&lt;li&gt;The Raspberry Pico will appear as a mass storage device on your computer.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Drag and Drop Firmware:

&lt;ul&gt;
&lt;li&gt;Open the folder containing the firmware file.&lt;/li&gt;
&lt;li&gt;Drag and drop the firmware file onto the mass storage device representing the Pico.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Wait for Flashing:

&lt;ul&gt;
&lt;li&gt;The Pico will automatically reboot and start flashing the new firmware.&lt;/li&gt;
&lt;li&gt;The ACT (Activity) LED on the Pico will blink rapidly during the flashing process.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Reconnect Pico:

&lt;ul&gt;
&lt;li&gt;After the flashing is complete, disconnect the Pico from your computer.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Power Cycle Pico:

&lt;ul&gt;
&lt;li&gt;Reconnect the Pico to power or press the RESET button to start running the new program.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Upload our code
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Make sure you have enabled MicroPython support and selected desired device.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mEcFmiH7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/dhqoqqstc/image/upload/v1703450005/blog/blinking_led/mlctuav7crw6p5ve64dr.webp" alt="Settings" width="800" height="585"&gt;
&lt;/li&gt;
&lt;li&gt;To upload our code, right click &lt;code&gt;main.py&lt;/code&gt; file and then select &lt;code&gt;Run 'Flash main.py'&lt;/code&gt; or simply
press &lt;code&gt;CTRL+SHIFT+F10&lt;/code&gt;.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i2HaMmst--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/dhqoqqstc/image/upload/v1703450005/blog/blinking_led/hdfwzjldedmzc1rblql0.webp" alt="Settings" width="788" height="261"&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Troubleshooting / Tips
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Bootloader Mode Button: If you encounter issues entering bootloader mode, double-check that you are holding down the
BOOTSEL button while connecting the Pico to your computer.&lt;/li&gt;
&lt;li&gt;Verify Flashing: Some firmware files may include a verification step. Ensure that the flashing process completes
without errors, and the ACT LED indicates successful activity.&lt;/li&gt;
&lt;li&gt;Check Firmware Compatibility: Ensure that the firmware you are flashing is compatible with the Raspberry Pico and
its hardware specifications.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you end up with &lt;code&gt;failed to access /dev/ttyACM0&lt;/code&gt; error then you will need to adjust permissions:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt; &lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-la&lt;/span&gt; /dev/ttyACM0
 crw-rw---- 1 root dialout 166, 0 Jul 28 19:51 /dev/ttyACM0
 &lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;chmod &lt;/span&gt;777 /dev/ttyACM0
 &lt;span class="c"&gt;# or&lt;/span&gt;
 &lt;span class="nv"&gt;$ &lt;/span&gt;usermod &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="nt"&gt;-G&lt;/span&gt; dialout &lt;span class="nv"&gt;$USER&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; reboot
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Tests
&lt;/h2&gt;

&lt;p&gt;After testing, we confirm that the blinking LED using MicroPython on the Raspberry Pico works as intended. The&lt;br&gt;
results give us confidence in the reliability of the code and the accuracy of GPIO control. This success lays a solid&lt;br&gt;
foundation for future projects and developments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0sih4NkK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://res.cloudinary.com/dhqoqqstc/image/upload/v1690661594/blog/blinking_led/zozgbpie5tijiad9b1io.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0sih4NkK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://res.cloudinary.com/dhqoqqstc/image/upload/v1690661594/blog/blinking_led/zozgbpie5tijiad9b1io.gif" alt="Blinking LED" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In conclusion, this article explored the foundational steps of using MicroPython on the Raspberry Pico to control an&lt;br&gt;
LED. By importing essential modules such as time and machine, and leveraging the Pin class, we initialized the LED pin&lt;br&gt;
and implemented a straightforward blinking pattern. The code, encapsulated in an infinite loop, demonstrated the&lt;br&gt;
sequential activation and deactivation of the LED with half-second intervals, providing a hands-on introduction to basic&lt;br&gt;
GPIO operations. This exercise serves as a fundamental building block for more complex projects and serves as a&lt;br&gt;
practical starting point for those delving into embedded systems development with MicroPython on the Raspberry Pico.&lt;/p&gt;

&lt;p&gt;The complete code is available on&lt;br&gt;
my &lt;a href="https://github.com/Porok12/blog-examples/tree/master/raspberry-pico-python-blinking-led"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.raspberrypi.com/documentation/microcontrollers/micropython.html"&gt;RaspberryPI Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://micropython.org/download/?vendor=Raspberry%20Pi"&gt;MicroPython&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>raspberrypi</category>
      <category>python</category>
      <category>iot</category>
      <category>embedded</category>
    </item>
  </channel>
</rss>
