DIY Projects - ElectronicsHub In-Depth Guides| Simple DIY Vedios| Insightful Reviews | Kits Mon, 20 Mar 2023 09:18:07 +0000 en-US hourly 1 https://www.electronicshub.org/wp-content/uploads/2021/03/electronicshub-Favicon-150x150.png DIY Projects - ElectronicsHub 32 32 Automatic Room Lights using Arduino and PIR Sensor https://www.electronicshub.org/automatic-room-lights-using-arduino-pir-sensor/ https://www.electronicshub.org/automatic-room-lights-using-arduino-pir-sensor/#comments Sat, 25 Feb 2023 07:28:59 +0000 https://www.electronicshub.org/?p=17339 In this project, we will see the Automatic Room Lights using Arduino and PIR Sensor, where the lights in the room will automatically turn ON and OFF by detecting the presence of a human. Such Automatic Room Lights can be implemented in your garages, staircases, bathrooms, etc. where we do not need continuous light but […]

The post Automatic Room Lights using Arduino and PIR Sensor appeared first on ElectronicsHub.

]]>
In this project, we will see the Automatic Room Lights using Arduino and PIR Sensor, where the lights in the room will automatically turn ON and OFF by detecting the presence of a human.

Such Automatic Room Lights can be implemented in your garages, staircases, bathrooms, etc. where we do not need continuous light but only when we are present.

Also, with the help of an automatic room light control system, you need not worry about electricity as the lights get automatically off when there is no person.

So, in this DIY project, we have implemented Automatic Room Lights using Arduino and PIR Sensor. 

Overview

Automatic Room Lights using Arduino and PIR Sensor Image 1Automatic Room Lights System using Arduino is a very useful project as you need not worry about turning on and off the switches every time you want to turn on the lights. The main components of the Automatic Room Lights project are Arduino, PIR Sensor and the Relay Module.

Out of the three components, the PIR Sensor is the one in focus as it is the main device that helps in detecting humans and human motion.

In fact, the Automatic Room Lights project can be considered as one major application of the PIR Sensor. A similar concept is being already implemented in automatic toilet flush valves, hand dryers, etc.

Also Read: ARDUINO PIR SENSOR TUTORIAL

Circuit Diagram of Automatic Room Lights using Arduino

The following image shows the circuit diagram of the project implemented using Arduino UNO, PIR Sensor and a Relay Module.

Automatic Room Lights using Arduino and PIR Sensor Circuit Diagram 1

If you do not have a relay module, you can make one yourself using very simple hardware. The following circuit diagram shows the project being implemented with the help of discrete components for the Relay Module.

Automatic Room Lights using Arduino and PIR Sensor Circuit Diagram 2

CAUTION: The project involves connection with 230V AC Mains (or 110V, depending on where you live!!!). Be extremely careful when connecting the bulb and Relay to mains supply. If you are unfamiliar with the connections, I strongly recommend having an adult supervision (or an expert supervision).    

Components Required for Automatic Room Lights using Arduino

  • Arduino UNO [Buy Here]
  • PIR Sensor 
  • 5V Relay Module (Relay Board) 
  • LED 
  • 100Ω Resistor (1/4 Watt)  
  • Connecting Wires 
  • Breadboard 
  • Power Supply 

If you do not have a Relay Module, use the following components:

  • 5V Relay  
  • 2N2222 (or BC547) NPN Transistor 
  • 1N4007 PN Junction Diode 
  • 1KΩ Resistor (1/4 Watt) 

Component Description

PIR Sensor

We have already seen about PIR Sensor in the PIR Motion Sensor Tutorial and also implemented in a variety of projects like Home Security System and Automatic Door Opener.

Relay Module

A Relay Module is a very useful component as it allows Arduino, Raspberry Pi or other Microcontrollers to control big electrical loads. We have used a 2-channel Relay Module in this project but used only one relay in it. The relay module used in this project is shown below.

Relay Module

In order to control a single relay on the board, we need to use three pins of the relay module: VCC, GND and IN1. 

Before continuing, read How to use 5V Relay on Arduino

NOTE: The relay module used in this project is as active LOW one i.e. when the IN1 pin is HIGH, the relay is OFF and when it is LOW, the relay is activated. This point is important while programming the Arduino UNO. 

Circuit Design

PIR Sensor’s Data OUT Pin is connected to Arduino’s Digital I/O Pin 8. An LED is connected to pin 13 of Arduino to indicate whether the light is turned ON or OFF.

The IN1 pin of the Relay Module is connected to Pin 9 of Arduino. A bulb is connected to mains supply through relay. One terminal of the bulb is connected to one wire of the mains supply. The other terminal of the bulb is connected to the NO (Normally Open) contact of the Relay Module.

COM (Common) contact of the Relay is connected to the other wire of the mains supply. Be careful when connecting this part of the project.   

Code

The code for the Automatic Room Lights using Arduino and PIR Sensor is given below.

Working of the Project

The Automatic Room Lights using Arduino and PIR Sensor is a simple project, where the lights in the room will automatically turn on upon detecting a human motion and stay turned on until the person has left or there is no motion.

Working of this project is very simple and is explained here.

Initially, when there is no human movement, the PIR Sensor doesn’t detect any person and its OUT pin stays LOW. As the person enters the room, the change in infrared radiation in the room is detected by the PIR Sensor.

As a result, the output of the PIR Sensor becomes HIGH. Since the Data OUT of the PIR Sensor is connected to Digital Pin 8 of Arduino, whenever it becomes HIGH, Arduino will activate the relay by making the relay pin LOW (as the relay module is an active LOW module).

This will turn the Light ON. The light stays turned ON as long as there is movement in front of the sensor.Automatic Room Lights using Arduino and PIR Sensor Image 2

If the person takes a nap or leaves the room, the IR Radiation will become stable (there will be no change) and hence, the Data OUT of the PIR Sensor will become LOW. This in turn will make the Arduino to turn OFF the relay (make the relay pin HIGH) and the room light will be turned OFF.Automatic Room Lights using Arduino and PIR Sensor Image 3

Applications

I’ve already mentioned a few applications of the Automatic Room Lights concept. Some of them are:  

  • Garage Lights
  • Bathroom Lights
  • Hand Dryers
  • Toilet Flushers
  • Security Lights

The post Automatic Room Lights using Arduino and PIR Sensor appeared first on ElectronicsHub.

]]>
https://www.electronicshub.org/automatic-room-lights-using-arduino-pir-sensor/feed/ 55
A Beginner’s Guide for Programming Raspberry Pi Pico in Windows https://www.electronicshub.org/programming-raspberry-pi-pico-in-windows/ https://www.electronicshub.org/programming-raspberry-pi-pico-in-windows/#comments Mon, 29 Mar 2021 11:14:26 +0000 https://www.electronicshub.org/?p=2035115 In this tutorial, we will see how to install and setup Visual Studio Code for Programming Raspberry Pi Pico in Windows System. We will learn how to install the necessary toolchain on the Windows, download and install VS Code, download the Raspberry Pi Pico SDK and build a demo project from Visual Studio Code in […]

The post A Beginner’s Guide for Programming Raspberry Pi Pico in Windows appeared first on ElectronicsHub.

]]>
In this tutorial, we will see how to install and setup Visual Studio Code for Programming Raspberry Pi Pico in Windows System. We will learn how to install the necessary toolchain on the Windows, download and install VS Code, download the Raspberry Pi Pico SDK and build a demo project from Visual Studio Code in Windows.

What Tools We Need to Install?

Programming Raspberry Pi Pico from a Windows System is a little bit different (or at least the initial setup part) as you have to download and install all the necessary tools one-by-one instead of simple commands as we did in a Linux (Raspberry Pi OS) system.

Before installing Visual Studio Code, you have to download the following tools:

  • ARM GCC Compiler
  • CMake
  • Build Tools for Visual Studio Code
  • Python
  • Git

After installing these tools, we can install the Visual Studio Code, configure it and start Programming Raspberry Pi Pico in Windows System.

ARM GCC Compiler

We will begin our setup process for Programming Raspberry Pi Pico in Windows with downloading and installing the GCC Compiler for ARM Cortex Processors. Go to the download page of GNU Arm Embedded Toolchain, scroll down and download the latest version of Windows executable.

Pico-Windows-ARM-GCC-1

After downloading, open the downloads folder and double click on the executable to begin the installation process. The installation is very simple and you can simply follow the onscreen instructions.

Pico-Windows-ARM-GCC-2

On the final step, make sure that you check the “Add path to environment variable” option.

Pico-Windows-ARM-GCC-3

CMake

The next important tool we are going to download and install is the CMake. Go to the official CMake download page and in the ‘Platform’ section under Binary distributions, download the 64-bit Windows Installer (.msi file).

I am sure, most of the Windows computers are 64-bit systems but in case you are struck with a 32-bit machine, download the ‘i386’ Installer.

Pico-Windows-CMake-1

Double click on downloaded installer file to begin installation. Again, the installation process is very simple. When asked by the CMake installer, select the option “Add CMake to system PATH” option.

Pico-Windows-CMake-2

Build Tools for Visual Studio Code

Next, we will download and install Build Tools for Visual Studio 2019. This tool is important for using command line interface. In the official download page, expand “Tools for Visual Studio 2019”, scroll down and download “Build Tools for Visual Studio 2019”.

Pico-Windows-Visual-Studio-1

A small executable will be downloaded. Double click on this executable to begin installation process. When the installer begins, you will get a window to configure the installation options. Select “C++ Build Tools” and on the right, make sure that “Windows 10 SDK” is selected (important).

Pico-Windows-Visual-Studio-2

After selecting the necessary options, click on install. It is a significantly large install (around 6.5 GB). So, grab a cup of coffee, sit back and wait for installation to complete.

Installing Python

In order to program Raspberry Pi Pico using VS Code, you need to install Python in your computer. Go to the Python download page and click on Windows Installer (64-bit). At the time of writing this tutorial, the latest version of Python was 3.9.2.

Pico-Windows-Python-1

Double click on the executable file, which was “python-3.9.2-amd64” in my case to begin the installation procedure. In the first screen itself, you get a “Install Now” button. But before clicking on that make sure to check the “Add Python 3.9 to PATH” option at the bottom. Now you can click on the “Install Now” button.

Pico-Windows-Python-2

Once the Python installation is complete, you will get a “Setup was successful” message. In the same screen, select the “Disable path length limit” option and then click on close.

Pico-Windows-Python-3

Git

The last tool we have to install is Git. Go to the Git download page and download the “64-bit Git for Windows Setup” executable file.

Pico-Windows-Git-1

Once you begin the installation, it will ask you to select a default editor for Git, make sure that Vim is not selected. I chose Notepad++ as the default editor.

Pico-Windows-Git-2

After this, there are some important options you have to change. I will mention all the settings which are necessary and the remaining are assumed to be default.

Select “Git from the command line and also from 3rd-party software” option.

Pico-Windows-Git-3

Then select, “Checkout as-is, commit as-is” option.

Pico-Windows-Git-4

Select “Use Windows’ default console window” option.

Pico-Windows-Git-5

Finally, check “Enable experimental support for pseudo consoles” and click on install.

Pico-Windows-Git-6

Download Pico SDK and Examples

After installing all the tools mentioned above, we are now ready to download the official Raspberry Pi Pico SDK and also the examples. I created a folder called “pico” in C drive. You can use any directory to download the SDK.

Open the command prompt in your Windows system and change to the directory where you want to download the SDK and Examples (in my case, it is C:\pico).

First, clone the ‘pico-sdk’ using the following command.

git clone -b master https://github.com/raspberrypi/pico-sdk.git

Pico-Windows-SDK-1

Open the ‘pico-sdk’ directory and add the tinyUSB submodule (important to use USB).

cd pico-sdk
git submodule update –init

Pico-Windows-SDK-2

Now, go back to the previous directory and download ‘pico-examples’

cd ..
git clone -b master https://github.com/raspberrypi/pico-examples.git

Programming Raspberry Pi Pico from Command Line

To test the tools and start with our first build, let us see how to program the Raspberry Pi Pico for Windows’ Command Prompt. Click on start button and scroll down to “Visual Studio 2019”, expand it and click on “Developer Command Prompt for VS 2019” option.

Pico-Windows-CMD-1

Change to the ‘pico’ directory where you downloaded the SDK and examples. Set the path for SDK using the following command.

cd \
cd pico
setx PICO_SDK_PATH “..\..\pico-sdk”

Pico-Windows-CMD-2

Once the path is set, exit from the command prompt (by typing exit) and restart the same command prompt (Developer Command Prompt for VS 2019). This will ensure that the command prompt has correct path set.

Now, in the pico folder, create a ‘build’ directory and change to the build directory.

Pico-Windows-CMD-3

We can now start using CMake to build the Makefiles. Use the following command.

cmake -G “NMake Makefiles” ..

Pico-Windows-CMD-4

After this, we can now build our projects using nmake command.

nmake

NOTE: If you simply use ‘nmake’ command, it will build for all the projects and will take some time. Alternatively, you can specify the project you want build in the nmake command.

Browse to the specific project in the build folder, where you can find the executable in different formats like .elf, .uf2, etc. To upload the program to Raspberry Pi Pico using drag-and-drop method, use the .uf2 file.

Installing Visual Studio Code in Windows

We are now ready to install Visual Studio Code and begin Programming Raspberry Pi Pico in Windows from VS Code. Go to the downloads page of Visual Studio Code and click on the “Windows” download option. This will download the 64-bit installer.

VS-Code-Install-1

Begin installation buy double clicking on the installer and follow the instructions. When asked, check the “Add to PATH” option.

VS-Code-Install-3

The remaining instructions are very simple. Do not launch the Visual Code yet. After installation, open the “Developer Command Prompt for VS 2019” like we did in the previous section.

Type “code” and hit enter to launch the Visual Studio Code with all the correct settings and environment variables.

Pico-Windows-VS-Code-3

NOTE: This is the correct way to launch Visual Studio Code for Programming Raspberry Pi Pico in Windows system. Before proceeding further, delete the contents in the ‘build’ folder as there is a conflict in the text editor.

First thing we need to do after launching VS Code is to install CMake extension. On the left side bar, select ‘Extensions’ and search for ‘CMake Tools’. From the results, select CMake Tools and click on install.

Pico-Windows-VS-Code-4

Now, on the left bottom corner, click on ‘Settings’ icon and select Settings.

Pico-Windows-VS-Code-5

Expand the ‘Extensions’ option and select ‘CMake Tool configuration’ option. On the right, scroll and select ‘Cmake: Configure Environment’ and set Item as ‘PICO_SDK_PATH’ and Values as ‘..\..\pico-sdk’.

Pico-Windows-VS-Code-6

Scroll down further until you find ‘ Cmake: Generator ’. Enter ‘ NMake Makefiles ’ in the space below. You can close the settings window.

Pico-Windows-VS-Code-7

Now, select ‘Explorer’ tab on the top left and click on ‘Open Folder’ option. Browse to c:\pico\pico-examples\ and select the folder.

Pico-Windows-VS-Code-8

CMake will ask you to configure the project. Select yes and then choose “ GCC for arm-none-eabi ” option.

Pico-Windows-VS-Code-9

If you missed this selection, use the ‘No Kit Selected’ from the bottom blue status bar.

Pico-Windows-VS-Code-10

Now, the CMake tool will start building the Makefiles for all the projects. You can build the projects by selecting the ‘Build’ option from the bottom blue status bar. Next to this ‘Build’ option, you can select the particular project you want to build to.

Pico-Windows-VS-Code-11

Uploading the code to Raspberry Pi Pico is the same as before.

Conclusion

A complete step-by-step beginner’s guide on programming Raspberry Pi Pico in Windows System. You learned what are the necessary tools you have download and install, how to configure those tools during installation or after installation, how to download Pico SDK and examples, build projects from Windows Command Prompt and also how to Program Raspberry Pi Pico in Windows using Visual Studio Code.

The post A Beginner’s Guide for Programming Raspberry Pi Pico in Windows appeared first on ElectronicsHub.

]]>
https://www.electronicshub.org/programming-raspberry-pi-pico-in-windows/feed/ 8
ESP32 BMP180 Sensor Tutorial | How to Interface BMP180 with ESP32? https://www.electronicshub.org/esp32-bmp180-tutorial/ https://www.electronicshub.org/esp32-bmp180-tutorial/#comments Mon, 29 Mar 2021 10:41:37 +0000 https://www.electronicshub.org/?p=2035104 In this tutorial, we will learn how to interface BMP180 with ESP32 DevKit Development Board. If you want to build a Web based Pressure and Temperature Monitoring System using ESP32, then BMP180 is one of the cheapest choice available (there are definitely better sensors than BMP180, like BME280 for example). None the less, learn how […]

The post ESP32 BMP180 Sensor Tutorial | How to Interface BMP180 with ESP32? appeared first on ElectronicsHub.

]]>
In this tutorial, we will learn how to interface BMP180 with ESP32 DevKit Development Board. If you want to build a Web based Pressure and Temperature Monitoring System using ESP32, then BMP180 is one of the cheapest choice available (there are definitely better sensors than BMP180, like BME280 for example). None the less, learn how ESP32 BMP180 Pressure and Temperature Sensor interface works, setup Arduino IDE, display the pressure and temperature data on an LCD. Additionally, you can design a simple ESP32 Web Server which continuously displays the Pressure and temperature from BMP180 Sensor.

ESP32-BMP180-Image-2

A Brief Note on BMP180 Sensor

We have already seen about BMP180 Sensor in Arduino BMP180 Tutorial. But let us recap some important info about the sensor. The BMP180 is a Digital Barometric Pressure Sensor from Bosch. It is an ultra-low power device based on piezo resistive MEMS device. It can measure the atmospheric pressure in the range of 300 hPa to 1100 hPa.

In addition to measuring pressure, the BMP180 Sensor can also measure Temperature in the range of 00C to 650C. Since BMP180 measures the atmospheric pressure, you can easily deduct the altitude of the point (as they both are related).

BMP180-Module-Front

The actual BMP180 Sensor is a tiny device with dimensions 3.6mm x 3.8mm. It is available in 7 pin LGA package and communicates with a Microcontroller either through I2C or SPI (only one is possible in the final sensor).

An important point about sensors is its calibration. A sensor needs to be properly calibrated to produce acceptable results. Fortunately, all the Bosch BMP180 Barometric Pressure Sensors are factory calibrated so that they are ready to use.

Many third-party module and sensor manufacturers took the tiny BMP180 Sensor and designed a small Module, which is perfectly suitable for Arduino and other DIY Projects. Most of the modules available today use I2C Communication. [ESP32 Projects for Beginners]

Pin Diagram of BMP180

The following image shows the Pins of BMP180 Sensor. The module has labelled the pins on the back.

BMP180-Module-Back

As you can see, there are four pins on the BMP180 Module:

  • VIN
  • GND
  • SCL
  • SDA

ESP32 BMP180 Interface

Now that we have seen a little bit about BMP180 Pressure and Temperature Sensor, let us now proceed with understanding how to interface BMP180 with ESP32. The first thing you have to remember is that BMP180 is a Digital Sensor.

The next important thing to remember is that the BMP180 Sensor uses I2C bus for communication with a microcontroller. If you remember the Pinout of ESP32, the default I­2C pins of ESP32 are GPIO 21 (SDA) and GPIO 22 (SCL). They are labelled as D21 and D22 respectively on the ESP32 DevKit Development Board.

So, we have to use these pins to interact with BMP180 Sensor.

Components Required

  • ESP32 DevKit Development Board
  • BMP180 Pressure and Temperature Sensor (Module)
  • 16×2 LCD
  • PCF8574 I2C LCD Module
  • Breadboard
  • Connecting Wires
  • Micro USB Cable

Circuit Diagram

The following image shows the connections between ESP32 and BMP180. The BMP180 Module has on-board 3.3V regulator. So, you can power the board with 5V. Connect VIN of BMP180 to VIN of ESP32 Board. Also connect the GND pin of BMP180 to any GND pin of ESP32.

Now, coming to the I2C Pins, connect SDA to GPIO 21 of ESP32 (which is marked as D21 on Board) and SCL to GPIO22 (which is marked as D22). This finishes all the connections.

ESP32-BMP180-Circuit

Preparing Arduino IDE

You need to download one library associated with BMP180 Sensor so that ESP32 will properly communicate with the Sensor. Go to Tools -> Manage Libraries… in Arduino IDE.

BMP180-Library-1

In the search bar, enter ‘bmp180’ and install ‘Adafruit BMP085 Library’ by Adafruit.

BMP180-Library-2

Displaying Pressure and Temperature on Serial Monitor

After making the proper connections and installing the necessary libraries as mentioned above, we will now see how to read the Pressure and Temperature data from BMP180 Sensor using ESP32 and display the result on the Serial Monitor.

Code

The BMP180 Sensor uses I2C bus for communication with the Microcontroller. I wrote a simple code which will initialize the BMP180 Sensor and reads the Pressure and Temperature data from the sensor.

To view the result, I simply used the serial monitor to print the pressure values in hPa for and degree Celsius for temperature.

ESP32-BMP180-Image-3

The following image shows the screenshot of the Serial Monitor, which is continuously printing the pressure and temperature reading every 3 seconds.

ESP32-BMP180-Serial

ESP32 BMP180 with I2C LCD

Displaying pressure and temperature data from BMP180 Sensor on Serial Monitor is useful just for testing the connections and the code itself. To build a practical “Embedded System” application, you have to use a display module of some kind (OLED, 16×2 Character LCD, Nokia 5110 LCD, graphical LCD etc.) to view the pressure and temperature readings.

I used a regular 16×2 Character LCD Display Module in combination with PCF8574 I2C LCD Module to display the pressure and temperature readings from ESP32 BMP180 Sensor Interface.

I made a dedicated tutorial on how to use an I2C LCD with ESP32. Check out that tutorial for in-depth information. I also discussed the necessary libraries you have download to successfully connect I2C LCD with ESP32 in that tutorial.

NOTE: I also explained how to get the Slave Address of I2C LCD Module in that tutorial. This step is very important.

Circuit Diagram

The additional components you require are a 16×2 LCD Display and an I2C LCD Module (based on PCF8574). Plug-in the I2C LCD Module at the back of the 16×2 LCD Display. The I2C LCD Module needs only four connections (two of them are for power and two are for data).

All the necessary connections between ESP32 and I2C LCD Module as well as between ESP32 and BMP180 Pressure and Temperature Sensor are shown in the following circuit diagram.

ESP32-BMP180-LCD-Circuit

Code

The code for ESP32 BMP180 Sensor Interface with I2C LCD is very simple. The initialization part of the sensor is similar to the previous code. Only the LCD related code is additionally added.

ESP32-BMP180-Image-1

Conclusion

A complete beginner’s guide on interfacing BMP180 Pressure and Temperature Sensor with ESP32 is implemented here. You learned some basic information about BMP180 Sensor, how ESP32 BMP180 Sensor Interface works, necessary libraries for communicating with BMP180, how to display pressure and temperature data on Serial Output and also how to connect I2C LCD with ESP32 and display the pressure and temperature readings.

As far as a Web based Pressure and Temperature Monitoring System using ESP32 and BMP180 Sensor is considered, I will update this page shortly with the code for Web Server.

The post ESP32 BMP180 Sensor Tutorial | How to Interface BMP180 with ESP32? appeared first on ElectronicsHub.

]]>
https://www.electronicshub.org/esp32-bmp180-tutorial/feed/ 1
Learn How to Create Your Own New Project for Raspberry Pi Pico https://www.electronicshub.org/raspberry-pi-pico-new-project/ https://www.electronicshub.org/raspberry-pi-pico-new-project/#comments Mon, 29 Mar 2021 09:55:34 +0000 https://www.electronicshub.org/?p=2035083 In this tutorial, we will see how to create a new Raspberry Pi Pico Project from scratch. In all the previous tutorials on Raspberry Pi Pico till now, we used the projects in the ‘pico-examples’ directory which we downloaded from the official Raspberry Pi Pico github repository. But now, we will learn how to create […]

The post Learn How to Create Your Own New Project for Raspberry Pi Pico appeared first on ElectronicsHub.

]]>
In this tutorial, we will see how to create a new Raspberry Pi Pico Project from scratch. In all the previous tutorials on Raspberry Pi Pico till now, we used the projects in the ‘pico-examples’ directory which we downloaded from the official Raspberry Pi Pico github repository. But now, we will learn how to create a new Raspberry Pi Pico Project, what are the necessary files we have to include in our project folder and also how to program and debug our project both from terminal as well as Visual Studio Code.

Create the Project Folder

First, we will create a new directory which will contain all our project related files. To do that, change directory to /home/pi/pico and create a new project folder. The new project folder must be alongside the ‘pico-sdk’ directory. Let us name this folder as ‘myproject’, to keep it simple.

cd ~
cd pico/
mkdir myproject

Pico-New-Project-1

If you take a look at the contents of /pico directory, it should contain ‘pico-sdk’ and ‘myproject’ directories and other directories (pico-examples is also in the same path).

Pico-New-Project-2

After creating the project directory, open the directory.

cd myproject

Create the Project Source File

Every project must contain at least two files: one is the main project source file with an extension of .c, which contains the ‘main’ function and all the project related code and the other file is associated with CMake (we will see more about this file in the next section).

In the myproject folder, create a new file called myproject.c. You can give any name to the source file. I used vim to create and edit the .c file.

vim myproject.c

Pico-New-Project-3

This will open a blank .c file. You can add your program here. To start typing in vim, press ‘i’ and start typing the code. I made a small code (which is also in the getting Started with raspberry Pi Pico Guide) which will blink the on-board LED and also print ‘Hello, World!’ text on the Serial Output every second.

Pico-New-Project-4

After typing the code, press ‘Esc’ key and then type ‘:wq’ in vim to write to the file and quit vim. This is not a tutorial on how to use vim. I suggest you to make use of other tutorials if you are not familiar with vim and its commands.

Create CMakeLists File

If you remember in the ‘Raspberry Pi Pico Serial Programming’ tutorial, we have seen this CMakeLists.txt file. This is a configuration file for CMake, where you can set different parameters for the CMake tool.

So, in the ‘myproject’ directory, create a new file called CmakeLists.txt using vim.

vim CMakeLists.txt

Pico-New-Project-5

If you observe the commands in CMakeLists.txt file, it is pretty straight forward. Also, I am enabling the stdio output on both the UART as well as USB so that you can view the “Hello, World!” text printed on both the ports.

Pico-New-Project-6

Now, we have to copy ‘pico_sdk_import.cmake’ file from the /pico-sdk/external directory into our ‘myproject’ directory. Use the following command to copy the same.

cp ../pico-sdk/external/pico_sdk_import.cmake .

Pico-New-Project-7

Finally, the contents of ‘myproject’ should look something like this. It should contain three files:

  • myproject.c
  • CMakeLists.txt
  • pico_sdk_import.cmake

Pico-New-Project-8

Build the Project

After this, we are now ready to build the project. If you remember in the previous tutorial, we created a build directory and this directory contains all the executables for a project. We will do the same here. First create a ‘build’ directory in ‘myproject’ directory.

mkdir build

Pico-New-Project-9

Next, we have to set the path for SDK folder. We can do this using the export command.

export PICO_SDK_PATH=../../pico-sdk

Pico-New-Project-10

Using cmake command, we can now create the makefiles of the project.

cmake ..

Pico-New-Project-11

Finally, we can build the project using make command.

make -j4

Pico-New-Project-12

Contents of Build Folder

If you list out the contents of the build folder, you can see that it contains several files and some of them are executable files. Of all these files, we are interested in two files with extensions .elf and .uf2.

Pico-New-Project-13

In the previous tutorials, if you remember them, we used .uf2 file to drap-and-drop the executable into Raspberry Pi Pico, when it is emulated as a removable storage device (plugging in Raspberry Pi Pico while holding the BOOTSEL button).

In the SWD tutorial, we used the SWD interface to program Raspberry Pi Pico but this time we used the .elf file. In this tutorial, we will use the SWD interface to upload the program to Raspberry Pi Pico.

Program Raspberry Pi Pico using SWD

Shutdown the Raspberry Pi and make the connections between Raspberry Pi Pico’s SWD interface and Raspberry Pi. The following table will remind you the connections between them.

Raspberry Pi Pico

Raspberry Pi
SWDIO

GPIO 24 (PIN 18)

SWD GND

GND (PIN 20)
SWCLK

GPIO 25 (PIN 18)

After making the connections, power on the Raspberry Pi and plug-in Raspberry Pi Pico into one of the USB Ports of Raspberry Pi. This will act as a power supply to Pico as well as a serial port.

Open the terminal and browse to build directory in myproject directory. Here, use the following command to upload the code to Raspberry Pi Pico.

openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg -c “program myproject.elf verify reset exit”

Pico-New-Project-14

Once the program is uploaded, you will get a confirmation as ‘Programming Finished’, ‘Verified OK’ and ‘Resetting Target’.

Pico-New-Project-15

You can see the LED blinking. Since we are also printing “Hello, World!” on the serial output, we will use the USB Port to view this. You can use Minicom to view the output of the USB Port using the following command.

minicom -b 115200 -o -D /dev/ttyACM0

Pico-New-Project-16

IMPORTANT NOTE: I skipped the debugging and programming Raspberry Pi Pico using VS Code as I already discussed them in the previous tutorials. You can refer to those tutorials and apply the same methods to your own project for programming through VS Code as well as debugging.

Conclusion

A complete step-by-step tutorial for beginners on creating a new project for Raspberry Pi Pico. You learned how to create a new Raspberry Pi Pico project, what are the essential files to have in the project folder, build the newly created project and upload the program using SWD interface.

The post Learn How to Create Your Own New Project for Raspberry Pi Pico appeared first on ElectronicsHub.

]]>
https://www.electronicshub.org/raspberry-pi-pico-new-project/feed/ 2
How to Program Raspberry Pi Pico with Visual Studio Code? https://www.electronicshub.org/program-raspberry-pi-pico-with-visual-studio-code/ https://www.electronicshub.org/program-raspberry-pi-pico-with-visual-studio-code/#comments Mon, 29 Mar 2021 09:27:04 +0000 https://www.electronicshub.org/?p=2035064 In this tutorial, we will learn how to Program Raspberry Pi Pico with Visual Studio Code. Visual Studio Code or simply VS Code is a great IDE for software development. We will see how to install VS Code on Raspberry Pi (the host computer) and in a later tutorial we will see the same for […]

The post How to Program Raspberry Pi Pico with Visual Studio Code? appeared first on ElectronicsHub.

]]>
In this tutorial, we will learn how to Program Raspberry Pi Pico with Visual Studio Code. Visual Studio Code or simply VS Code is a great IDE for software development. We will see how to install VS Code on Raspberry Pi (the host computer) and in a later tutorial we will see the same for a Windows system (including installation of all the other necessary tools).

Introduction

In all the previous Raspberry Pi Pico tutorial, we used Raspberry Pi as the main host computer and used the terminal to program Raspberry Pi Pico in C. This is okay if your application is just to Blink and LED or print some info on the Serial Output.

But if you want to implement a bigger project, using terminal to write and maintain the code becomes tedious (at least for beginners). This is where the like of VS Code and Eclipse IDEs come into play.

The Raspberry Pi Foundation recommends Visual Studio Code IDE to edit and debug the code in Raspberry Pi 4 SBC.

VS Code or Visual Studio Code is a code editing software developed by Microsoft. It is available for all the three major OS platforms: Windows, Linux and Mac. In addition to the basic functionality of code editor, VS Code also has several important features like Debugging, highlighting syntax, support for plugins etc.

First, we will see all the step for installing Visual Studio Code on our Raspberry Pi host computer. Then, we will shift our focus to a Windows System, where we need to download (and install) a whole lot of tools before installing VS Code. So, let’s get started.

Installing Visual Studio Code in Raspberry Pi

IMPORTANT NOTE: Before proceeding with installing VS Code on your Raspberry Pi host computer, I strongly recommend you to complete the previous tutorials on Raspberry Pi Pico. At least the ‘Programming Raspberry Pi Pico with C’ and ‘Program and Debug Raspberry Pi Pico with SWD’ tutorials.

This is because, I discussed all the necessary tools that you have to install in your Raspberry Pi in order to program Pico with C Programming Language.

All the following steps assume that you have already installed the necessary toolchains as mentioned in the previous tutorials.

First, open the browser in your Raspberry Pi and go to the official VS Code download website. Scroll down to Linux Downloads and download the ARM version of VS Code in .deb package (64-bit or 32-bit depending on your Raspberry Pi OS).

I selected the .deb package for ARM (not ARM 64) and at the time of writing this tutorial, it downloaded a file called ‘code_1.54.3-1615805722_armhf.deb’.

Pico-VS-Code-Pi-1

Open the downloads folder in the Raspberry Pi and double click on the downloaded .deb file to begin the installation of VS Code.

Pico-VS-Code-Pi-2

It will ask for password. Enter your Raspberry Pi’s password. The installation is pretty straight forward and just follow the on-screen instructions.

Before launching VS Code, you have to install three extension for Visual Studio Code. Use the following commands to install these extensions one after the other.

code –install-extension marus25.cortex-debug
code –install-extension ms-vscode.cmake-tools
code –install-extension ms-vscode.cpptools

Pico-VS-Code-Pi-3

Start Visual Studio Code

After installing these extensions, you can launch Visual Studio Code using the command ‘code’ in the terminal. But before that, make sure that you set the path for ‘pico-sdk’.

export PICO_SDK_PATH=/home/pi/pico/pico-sdk
code

Pico-VS-Code-Pi-4

The VS Code IDE will now start and opens its home page.

Pico-VS-Code-Pi-5

Open the ‘pico-examples’ folder in VS Code by selecting the ‘Explorer’ option on the left and click on ‘Open Folder’. This will open the file explorer and browse to the ‘/home/pi/pico/pico-examples’ directory and click on OK.

Pico-VS-Code-Pi-6

You can see the ‘pico-examples’ directory populated on the left with all the sub-folders. Assuming you have installed the CMake extension for VS Code earlier, the IDE will automatically configure the projects folder. The CMake extension will ask you configure ‘pico-examples’ projects. Click on ‘Yes’.

Pico-VS-Code-Pi-7

If you missed this option, there will be a No Kit Selected option at the bottom Blue coloured Status Bar. Click on that. Then select ‘GCC for arm-none-eabi’ option.

Pico-VS-Code-Pi-8

Now, you can build the projects. Again, at the bottom status bar click on ‘Build’ option. Next to this ‘Build’ option, you can select the specific project by clicking on [all] or all the projects will be built. I selected the ‘Blink’ project to demonstrate the working.

In the status bar, there is an option to configure CMake to build for ‘Debug’ or ‘Release’ mode. By default, ‘Debug’ option is selected.

Pico-VS-Code-Pi-9

Uploading and Debugging with VS Code

There is a .json file provided by Visual Studio Code called ‘launch.json’, which will help us in uploading the code to Raspberry Pi Pico and also debugging the code. Additionally, there is a ‘settings.json’ file, which will configure CMake extension to work properly.

We have to copy these two files into our ‘pico-examples’ folder. In the ‘pico-examples’ directory, make a new directory called ‘.vscode’.

cd ~/pico/pico-examples
mkdir .vscode

Now, copy the ‘launch.json’ and ‘settings.json’ files into this directory.

cp ide/vscode/launch-raspberrypi-swd.json .vscode/launch.json
cp ide/vscode/settings.json .vscode/settings.json

Pico-VS-Code-Pi-10

To upload and debug the code, go to Debug Tool bar by pressing “Ctrl+Shift+D”. A ‘Run and Debug’ option will pop-up at the top. Press the green play button to build the code, upload the code to Raspberry Pi Pico and start debugging session.

Pico-VS-Code-Pi-11

Additional Debug Control options will appear on the top. Press the Blue play button to start the code (continue option in OpenOCD).

Pico-VS-Code-Pi-12

You can set breakpoints anywhere you want and check if your code is reaching those breakpoints.

Conclusion

A step – by – step tutorial on installing Visual Studio Code in Raspberry Pi and Programming Raspberry Pi Pico with Visual Studio Code or VS Code. You learned how to download and install VS Code in Raspberry Pi OS, install necessary extensions for VS Code, import our examples folder in VS Code environment, configure CMake extension to program and debug the code to Raspberry Pi Pico.

In a later tutorial, I will show the steps you need to follow to install Visual Studio Code in a Windows System. This is a little bit different as you have install several tools individually instead of running a simple command (like we did in the Raspberry Pi).

The post How to Program Raspberry Pi Pico with Visual Studio Code? appeared first on ElectronicsHub.

]]>
https://www.electronicshub.org/program-raspberry-pi-pico-with-visual-studio-code/feed/ 4
ESP32 DHT11 Tutorial | DHT11 Humidity Temperature Sensor with ESP32 https://www.electronicshub.org/esp32-dht11-tutorial/ https://www.electronicshub.org/esp32-dht11-tutorial/#comments Sat, 27 Mar 2021 14:55:11 +0000 https://www.electronicshub.org/?p=2035046 In this tutorial, we will learn how to interface DHT11 with ESP32 DevKit Development Board. If you want to build a Web based Temperature and Humidity Monitoring System using ESP32, then DHT11 and DHT22 are the best choice as the Sensor. Learn how ESP32 DHT11 Humidity and Temperature Sensor interface works, setup Arduino IDE, display […]

The post ESP32 DHT11 Tutorial | DHT11 Humidity Temperature Sensor with ESP32 appeared first on ElectronicsHub.

]]>
In this tutorial, we will learn how to interface DHT11 with ESP32 DevKit Development Board. If you want to build a Web based Temperature and Humidity Monitoring System using ESP32, then DHT11 and DHT22 are the best choice as the Sensor. Learn how ESP32 DHT11 Humidity and Temperature Sensor interface works, setup Arduino IDE, display the humidity and temperature on an LCD. Additionally, you can design a simple ESP32 Web Server which continuously displays the humidity and temperature from DHT11 Sensor.

ESP32-DHT11-Image-1

A Brief Note on DHT11 Humidity and Temperature Sensor

We already used DHT11 Temperature and Humidity Sensor in a couple of earlier projects involving Arduino, Raspberry Pi, ESP8266 and STM32F103C8T6. DHT11 is a digital Humidity and Temperature Sensor, which consists of a resistive type Humidity Sensor, an NTC Type Temperature Sensor and an 8-bit Microcontroller.[ESP32 Projects for Beginners]

DHT11 Sensor

It can measure Humidity in the range of 20% to 80% Relative Humidity and temperatures in the range of 00C to 500C. The microcontroller in the DHT11 Sensor performs all the ADC related stuff and provides the Digital data through a single wire.

Additionally, the DHT11 Temperature and Humidity Sensor can have a cable length up to 20 meters. This means you can easily implement a wired Sensor system which can go into lengthy places.

Pin Diagram and Pin Description of DHT11

The following image shows the Pins of DHT11and the table next shows the pin description of DHT11.

DHT11 Pinout

Pin of DTH11 Description
VCC Power Supply (3V to 5.5V)
DATA Input / Output Data. Must be pulled HIGH.
NC Not Connected
GND Ground

Decoding Data from DHT11

In the Arduino DHT11 Tutorial, I explained in detail how the data from DHT11 Sensor looks like and how to extract this data without using any library. This is very useful if you are writing your drivers for DHT11 Sensor for any other microcontroller.

I won’t go into the details but the output of DHT11 is a 40-bit data divided as 8-bit Relative Humidity Integer Data + 8-bit Relative Humidity Decimal Data + 8-bit Temperature Integer Data + 8-bit Temperature Decimal Data + 8-bit Checksum.

DHT11 Sensor Data

ESP32 DHT11 Interface

Now that we have seen a little bit about DHT11 Humidity and Temperature Sensor, let us now proceed with understanding how to interface DHT11 with ESP32. The first thing you have to remember is that DHT is a Digital Sensor (with an internal microcontroller performing the data acquisition and ADC conversion).

The next important thing to remember is it requires a single wire for communication. This means we can use any Digital GPIO Pin of ESP32 to send and receive data to / from DHT11 and we need only one wire for proper communication.

Components Required

  • ESP32 DevKit Development Board
  • DHT11 Humidity and Temperature Sensor (Sensor or Module)
  • 1 KΩ Pullup Resistor
  • 16×2 LCD
  • PCF8574 I2C LCD Module
  • Breadboard
  • Connecting Wires
  • Micro USB Cable

NOTE: I got a DHT11 Module with a 1 KΩ Pullup resistor on the Data Line already installed. If you are using just the sensor, then this pull-up resistor is important. A 5 KΩ Pullup is recommended by the manufacturer.

Circuit Diagram

The following image shows the connections between ESP32 and DHT11. First of all, the VCC of DHT11 is connected to VIN of ESP32 Board.

NOTE: The range of power supply for DHT11 is from 3 V to 5.5 V. So, you can power DHT11 with a 3.3V Supply from ESP32 Board as well.

Next, the DATA pin. This pin should be pulled HIGH. As I have a module with a 1 KΩ Pullup already connected, I don’t need to make any additional connections. In case you are using only the DHT11 Sensor, then connect a 4.7 KΩ Resistor between Data Pin and VIN (or 3.3V) of ESP32.

NOTE: The pullup voltage of Data Pin can be anywhere between 3 V to 5.5 V.

Now, the Data pin is connected to GPIO 16 of ESP32, which is labelled as RX2 on the ESP32 DevKit Board.

The third pin is not connected to anything. Finally, the GND pin is connected to any GND pin of ESP32.

ESP32-DHT11-Circuit

Preparing Arduino IDE

You need to download a couple of libraries so that ESP32 will properly communicate with DHT11 Sensor. First is main DHT11 Sensor Library. Go to Tools -> Manage Libraries… in Arduino IDE.

ESP32-DHT11-Arduino-1

In the search bar, enter ‘dht’. Scroll through the options and install ‘DHT sensor library’ by Adafruit.

ESP32-DHT11-Arduino-2

The next library is associated with the Adafruit itself. Search for ‘adafruit unified’, scroll down and install ‘Adafruit Unified Sensor’ library.

ESP32-DHT11-Arduino-3

Displaying Humidity and Temperature on Serial Monitor

After making the proper connections and installing the necessary libraries as mentioned above, we will now see how to read the humidity and temperature data from DHT11 Sensor using ESP32 and display the result on the Serial Monitor.

Code

I wrote a simple code which will assign a pin to DHT11 Sensor, initialize the DHT11 Sensor and reads the humidity and temperature data from the sensor.

To view the result, I simply used the serial monitor to print the temperature values in % for Humidity and degree Celsius for temperature.

ESP32-DHT11-Image-4

The following image shows the screenshot of the Serial Monitor, which is continuously printing the humidity and temperature reading every 3 seconds.

ESP32-DHT11-Arduino-4

ESP32 DHT11 with I2C LCD

Displaying humidity and temperature values from DHT11 Sensor on Serial Monitor is useful just for testing the connections and the code itself. To build a practical “Embedded System” application, you have to use a display module of some kind (OLED, 16×2 Character LCD, Nokia 5110 LCD, graphical LCD etc.) to view the humidity and temperature readings.

I used a regular 16×2 Character LCD Display Module in combination with PCF8574 I2C LCD Module to display the temperature readings from ESP32 DHT11 Interface.

I made a dedicated tutorial on how to use an I2C LCD with ESP32. Check out that tutorial for in-depth information. I also discussed the necessary libraries you have download to successfully connects I2C LCD with ESP32 in that tutorial.

NOTE: I also explained how to get the Slave Address of I2C LCD Module in that tutorial. This step is very important.

Circuit Diagram

The additional components you require are a 16×2 LCD Display and an I2C LCD Module (based on PCF8574). Plug-in the I2C LCD Module at the back of the 16×2 LCD Display. The I2C LCD Module needs only four connections (two of them are for power and two are for data).

All the necessary connections between ESP32 and I2C LCD Module as well as between ESP32 and DHT11 Humidity and Temperature Sensor are shown in the following circuit diagram.

ESP32-DHT11-I2C-LCD-Circuit

Code

The code for ESP32 DHT11 Interface with I2C LCD is very simple. The initialization part of the sensor is similar to the previous code. Only the LCD related code is additionally added.

ESP32-DHT11-Image-2

Conclusion

A beginner’s tutorial on interfacing DJT11 Humidity and Temperature Sensor with ESP32 is implemented here. You learned some basic information about DHT11 Sensor, how ESP32 DHT11 Interface works, necessary libraries for communicating with DHT11, how to display humidity and temperature data on Serial Output and also how to connect I2C LCD with ESP32 and display the humidity and temperature readings.

As far as a Web based Humidity and Temperature Monitoring System using ESP32 and DHT11 Sensor is considered, I will update this page shortly with the code for Web Server.

The post ESP32 DHT11 Tutorial | DHT11 Humidity Temperature Sensor with ESP32 appeared first on ElectronicsHub.

]]>
https://www.electronicshub.org/esp32-dht11-tutorial/feed/ 2
ESP32 DS18B20 Tutorial | DS18B20 Temperature Sensor with ESP32 https://www.electronicshub.org/esp32-ds18b20-tutorial/ https://www.electronicshub.org/esp32-ds18b20-tutorial/#comments Sat, 27 Mar 2021 13:29:19 +0000 https://www.electronicshub.org/?p=2035032 In this tutorial, we will learn how to interface DS18B20 with ESP32 DevKit Development Board. If you want to build a Web based Temperature Monitoring System using ESP32, then DS18B20 is an excellent choice for the Temperature Sensor. Learn how ESP32 DS18B20 Temperature Sensor interface works, setup Arduino IDE, display the temperature on an LCD. […]

The post ESP32 DS18B20 Tutorial | DS18B20 Temperature Sensor with ESP32 appeared first on ElectronicsHub.

]]>
In this tutorial, we will learn how to interface DS18B20 with ESP32 DevKit Development Board. If you want to build a Web based Temperature Monitoring System using ESP32, then DS18B20 is an excellent choice for the Temperature Sensor. Learn how ESP32 DS18B20 Temperature Sensor interface works, setup Arduino IDE, display the temperature on an LCD. Additionally, you can design a simple ESP32 Web Server which continuously displays the temperature.

ESP32-DS18B20-Image-1

A Brief Note on DS18B20 Temperature Sensor

We already used DS18B20 Temperature Sensor in a couple of earlier projects involving Arduino, Raspberry Pi and ESP8266. DS18B20 is a digital Temperature Sensor from Maxim Integrated, which can measure temperatures in the range of -550C to +1250C.[ESP32 Projects for Beginners]

Unlike other digital sensors, which often communicate over I2C or SPI, the DS18B20 Temperature Sensor uses 1-Wire Communication to interact with a Microcontroller.

Arduino DS18B20 Temperature Sensor

If you are not familiar with 1-Wire bus, it is a communication bus developed by Dallas Semiconductor, and as the name suggests, it requires only one wire to communicate with a Processor (well, 1 Data Line and 1 GND Wire).

Additionally, the DS18B20 Temperature Sensor can derive its power from the data wire itself (known as Parasite Power), without the need for providing any external power (through the VDD Pin).

Since the DS18B20 is a Digital Temperature Sensor, the temperature data is stored in the on-chip EEPROM. Some of the other important features of DS18B20 are:

  • User configurable resolution between 9-bits and 12-bits.
  • A 64-bit Serial Code which is unique to each DS18B20 Sensor.

Pin Diagram and Pin Description of DS18B20

The following image shows the Pin Diagram and the table next shows the pin description of DS18B20.

Arduino DS18B20 Temperature Sensor Pin Out

Pin of DS18B20 Description
GND Ground
DQ Input / Output Data. Must be pulled HIGH.
VDD Power Supply

Different Ways to Power DS18B20

I already discussed this in the Arduino DS18B20 Tutorial. There are a couple of ways in which you can power up DS18B20:

  • Normal Way (using VDD)
  • Parasite Power

Refer to Arduino DS18B20 Tutorial for more information. I will power up DS18B20 normally in this ESP32 DS18B20 Tutorial.

ESP32 DS18B20 Interface

Now that we have seen a little bit about DS18B20, let us now proceed with understanding how to interface DS18B20 with ESP32. First thing to remember is that DS18B20 is a Digital Sensor. The next important thing to remember is it uses 1-Wire Communication.

What does this mean for ESP32? This means we can use any Digital GPIO Pin of ESP32 to send and receive data to / from DS18B20 and we need only one wire for proper communication.

Components Required

  • ESP32 DevKit Development Board
  • DS18B20 Temperature Sensor
  • 16×2 LCD
  • PCF8574 I2C LCD Module
  • Breadboard
  • Connecting Wires
  • Micro USB Cable

Circuit Diagram

The following image shows the connections between ESP32 and DS18B20. First of all, I am powering DS18B20 with normal power. So, the VDD of DS18B20 is connected to VIN of ESP32 Board.

NOTE: The range of power supply for DS18B20 is from 3 V to 5.5 V. So, you can power DS18B20 with a 3.3V Supply from ESP32 Board as well.

Next, the GND pin is connected to any GND pin of ESP32. Finally, the DQ Pin. First of all, this pin should be pulled HIGH. So, I connected a 4.7 KΩ Resistor between DQ Pin and 3.3V.

NOTE: The pullup voltage can be anywhere between 3 V to 5.5 V.

Now, the DQ pin is connected to GPIO 16 of ESP32, which is labelled as RX2 on the ESP32 DevKit Board.

ESP32-DS18B20-Circuit

Preparing Arduino IDE

There are couple of libraries you need to download to properly communicate with DS18B20. First is related to the 1-Wire Bus. Go to Tools -> Manage Libraries… in Arduino IDE.

ESP32-DS18B20-Arduino-1

In the search bar, enter ‘onewire’. Scroll through the options and install ‘OneWire’ library by Jim Studt, Paul Stoffregan Et Al.

ESP32-DS18B20-Arduino-2

The next library is associated with the DS18B20 device itself. Search for ‘dallas’ and install ‘DallasTemperature’ by Miles Burton Et Al.

ESP32-DS18B20-Arduino-3

Displaying Temperature on Serial Monitor

After making the proper connections and installing the necessary libraries as mentioned above, we will now see how to read the temperature data from DS18B20 using ESP32 and display the result on the Serial Monitor.

Code

I wrote a simple code which will initialize the 1-Wire Communication on GPIO 16, assign this One Wire Communication to the DS18B20 Sensor and reads the temperature data.

To view the result, I simply used the serial monitor to print the temperature values in degree Celsius.

ESP32-DS18B20-Image-3

The following image shows the screenshot of the Serial Monitor, which is continuously printing the temperature reading every 3 seconds.

ESP32-DS18B20-Arduino-4

ESP32 DS18B20 with I2C LCD

Displaying temperature values on Serial Monitor is useful just for testing the connections and the code itself. If you want a more practical “Embedded System” application, then using a display module of any kind (OLED, 16×2 Character LCD, Nokia 5110 LCD, graphical LCD etc.) to view the temperature reading is the simplest approach.

To keep things simple, I used a regular 16×2 Character LCD Display Module in combination with PCF8574 I2C LCD Module to display the temperature readings from ESP32 DS18B20 Interface.

I made a dedicated tutorial on how to use an I2C LCD with ESP32. Check out that tutorial for in-depth information. I also discussed the necessary libraries you have download to successfully connects I2C LCD with ESP32 in that tutorial.

NOTE: I also explained how to get the Slave Address of I2C LCD Module in that tutorial. This step is very important.

Circuit Diagram

The additional components you require are a 16×2 LCD Display and an I2C LCD Module (based on PCF8574). Plug-in the I2C LCD Module at the back of the 16×2 LCD Display. The I2C LCD Module needs only four connections (two of them are for power and two are for data).

All the necessary connections between ESP32 and I2C LCD Module as well as between ESP32 and DS18B20 Temperature Sensor are shown in the following circuit diagram.

ESP32-DS18B20-I2C-LCD-Circuit

Code

The code for ESP32 DS18B20 Interface with I2C LCD is very simple. The initialization part of the sensor is similar to the previous code. Only the LCD related code is additionally added.

ESP32-DS18B20-Image-2

Conclusion

A beginner’s tutorial on interfacing DS18B20 Temperature Sensor with ESP32 is implemented here. You learned some basic information about DS18B20, how ESP32 DS18B20 Interface works, necessary libraries for communicating with DS18B20, how to display temperature on Serial Output and also how to connect I2C LCD with ESP32 and display the temperature readings.

As far as a Web based Temperature Monitoring System using ESP32 and DS18B20 is considered, I will update this page shortly with the code for Web Server.

The post ESP32 DS18B20 Tutorial | DS18B20 Temperature Sensor with ESP32 appeared first on ElectronicsHub.

]]>
https://www.electronicshub.org/esp32-ds18b20-tutorial/feed/ 1
Learn how to Program and Debug Raspberry Pi Pico with SWD https://www.electronicshub.org/programming-raspberry-pi-pico-with-swd/ https://www.electronicshub.org/programming-raspberry-pi-pico-with-swd/#comments Fri, 26 Mar 2021 13:55:56 +0000 https://www.electronicshub.org/?p=2034923 In this tutorial, we will learn how to upload a program to Raspberry Pi Pico using SWD and also debug with SWD. Till now, we have been using drag-and-drop method for uploading program to Pico. But if you want to use SWD Interface of Pico to program and debug, then this tutorial is for you. […]

The post Learn how to Program and Debug Raspberry Pi Pico with SWD appeared first on ElectronicsHub.

]]>
In this tutorial, we will learn how to upload a program to Raspberry Pi Pico using SWD and also debug with SWD. Till now, we have been using drag-and-drop method for uploading program to Pico. But if you want to use SWD Interface of Pico to program and debug, then this tutorial is for you. Learn the steps for Programming Raspberry Pi Pico with SWD and also debugging the code with SWD, OpenOCD and GDB.

Program-Raspberry-Pi-Pico-with-SWD-Image-1

A Brief Note on SWD

Have you ever worked on developing an Embedded System Application? If the answer is yes, then you might be familiar with the term ‘Debugging’. In embedded systems, which are mainly designed with a single task (or a very small number and specific set of tasks) in mind, the process of debugging and testing is very critical as we often work as low as CPU Register level.

There are several hardware and software solutions for debugging embedded systems. One such offering is called Serial Wire Debug or SWD in short. SWD is a Debug and Trace Port embedded into the silicon of most modern ARM based Microcontrollers and Microprocessors.

Using a SWD Probe (a small hardware often connected to SWD Port of the Microcontroller and maps them to USB) you can program the Flash of the Microcontroller, Debug the Firmware, Add Breakpoints, Stepping through the Code, etc. with just two wires.

The combination of SWD, GDB (GNU Debugger) and OpenOCD (an on-chip debugger which supports debugging, in-system programming and boundary scan for embedded systems) is a very powerful debugging setup, especially for ARM Cortex series of Processors.

Raspberry Pi Pico SWD Programming and Debug

Like all ARM Cortex processors, the Raspberry Pi Pico also has dedicated hardware for debugging via the SWD Interface. The two wires required for SWD Debugging are called SWDIO (bidirectional SWD Data) and SWCLK (SWD Clock).

On the Raspberry Pi Pico, the SWD Pins are separated from the rest of the GPIO Pins and are placed at the bottom of the Board.

Raspberry-Pi-Pico-SWD-Pins

The 2-wire SWD Interface of RP2040 on the Raspberry Pi Pico board allows you to do the following:

  • Upload program into External Flash or Internal SRAM.
  • Control the state of execution of the processor i.e., run, halt, step, set breakpoints, etc.
  • Access processors memory and IO peripherals (which are memory mapped) through the system bus.

Installing Tools in Raspberry Pi

As mentioned earlier, GDB and OpenOCD are required for debugging any ARM Cortex Processor. So, we will now install these two in our host system, which in my case is a Raspberry Pi running the latest Raspberry Pi OS.

OpenOCD

To understand the SWD Protocol and control the ARM Cortex Processor (two in case of RP2040), you need a special translator called OpenOCD. Let us now see how to install OpenOCD in Raspberry Pi.

NOTE: The following steps will install OpenOCD in /home/pi/pico/openocd.

cd ~
cd pico
sudo apt install automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0- dev

Pico-SWD-1

This will install all the tools require by OpenOCD. Next, we will clone the OpenOCD into our host and install OpenOCD. Enter the following commands one after the other.

git clone https://github.com/raspberrypi/openocd.git –recursive –branch rp2040 –depth=1
cd openocd
./bootstrap
./configure –enable-ftdi –enable-sysfsgpio –enable-bcm2835gpio
make -j4
sudo make install

I will take some time to build and install OpenOCD. Sit back and relax.

GDB

Next step is to install GDB. To install GDB Multi-Arch, use the following command:

sudo apt install gdb-multiarch

Pico-SWD-2

We will see how to debug using OpenOCD and GDB in the next section.

Wiring Raspberry Pi Pico and Raspberry Pi

Before see how to program Raspberry Pi Pico using SWD, you have to first properly wire the Raspberry Pi Pico.

IMPORTANT NOTE: I do not know the exact reason but before making the connections, I had to shutdown Raspberry Pi to successfully program Raspberry Pi Pico using SWD. I read in Raspberry Pi Forum that both the Raspberry Pi and the target i.e., Raspberry Pi Pico in this case must be powered down before connecting SWD pins.

Since Raspberry Pi Pico is connected to Raspberry Pi through USB (to power it up), all I had to do was shutdown Raspberry Pi, make the SWD Connections and then power on Raspberry Pi.

The following table shows all the necessary connections between Raspberry Pi and Raspberry Pi Pico that you need to make.

Raspberry Pi Pico

Raspberry Pi
SWDIO

GPIO 24 (PIN 18)

SWD GND

GND (PIN 20)
SWCLK

GPIO 25 (PIN 22)

Program-Raspberry-Pi-Pico-with-SWD-Image-2

Programming Raspberry Pi Pico with SWD

Let us use the ‘Blink’ program as an example to understand how Raspberry Pi Pico SWD Programming works. If you remember in the ‘Programming Raspberry Pi Pico with C’ tutorial, we already built the Blink program, which resulted in a few target files.

For drag-and-drop programming via USB, we used the .uf2 file. But OpenOCD uses .elf file to upload the program.

Use the following commands to Program Raspberry Pi Pico with SWD.

cd ~
cd pico
cd pico-examples/build/
make blink

Pico-SWD-3

openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg -c “program blink/blink.elf verify reset exit”

Pico-SWD-4

The above command will invoke OpenOCD to program the blink.elf file to Raspberry Pi Pico, reset the board and exit the OpenOCD. If everything goes well, your terminal should display something like this and the LED on Raspberry Pi Pico should start to blink.

Pico-SWD-5

Debugging Raspberry Pi Pico with SWD

Let us now see how to debug the code suing SWD, OpenOCD and GDB. We already installed GDB in the previous step. Now, to include the debug related settings in the build files, you have use the CMake directive ‘ -DCMAKE_BUILD_TYPE=Debug ’.

But before that, you need to remove the ‘build’ directory from ‘pico-examples’ directory and create a new ‘build’ directory. Use the following commands to build examples with Debug information.

cd ~/pico/pico-examples/
rm -rf build
mkdir build
cd build
export PICO_SDK_PATH=../../pico-sdk
cmake -DCMAKE_BUILD_TYPE=Debug ..

Pico-SWD-6

Let us use the ‘hello_world’ example and build for the serial variant. You cannot use USB based serial connection for SWD Debugging as the USB Device will be disconnected when stopping the processor core in debugging.

Also, the connections between Raspberry Pi Pico and Raspberry Pi to view the output of Raspberry Pi Pico’s UART Serial Output on Raspberry Pi is as follows:

Raspberry Pi Pico

Raspberry Pi
GPIO 0 (UART0_TX)

GPIO 15 (UART_RX0) PIN 10

GPIO 1 (UART0_RX)

GPIO 14 (UART_TX0) PIN 8
GND

GND (Pin 14)

After making the connections, open the hello_world UART directory and build it.

cd hello_world/serial
make -j4

Use OpenOCD to open the GDB Server.

openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg

Pico-SWD-7

Keep this terminal as it is and open another terminal window and browse to UART Serial directory in the build directory.

cd ~
cd pico/pico-examples/build/hello_world/serial/

Open GDB and connect to OpenOCD Server.

gdb-multiarch hello_serial.elf
(gdb) target remote localhost:3333

Pico-SWD-8

To load the program into the flash memory of Raspberry Pi Pico, use load command.

(gdb) load

Start running the code.

(gdb) monitor reset init
(gdb) continue

Pico-SWD-9

If you are familiar with GDB commands, you can explore them.

Conclusion

A complete tutorial on Programming and Debugging Raspberry Pi Pico with SWD. Learn how Raspberry Pi Pico SWD Interface works, the necessary connections between Raspberry Pi Pico and Raspberry Pi for SWD, program Raspberry Pi Pico with SWD, use GDB to debug Raspberry Pi Pico using SWD.

The post Learn how to Program and Debug Raspberry Pi Pico with SWD appeared first on ElectronicsHub.

]]>
https://www.electronicshub.org/programming-raspberry-pi-pico-with-swd/feed/ 6
How to use BLE in ESP32? ESP32 BLE (Bluetooth Low Energy) Tutorial https://www.electronicshub.org/esp32-ble-tutorial/ https://www.electronicshub.org/esp32-ble-tutorial/#comments Wed, 24 Mar 2021 13:59:11 +0000 https://www.electronicshub.org/?p=2034782 In this tutorial, we will learn about the BLE feature in ESP32. ESP32 has Bluetooth low Energy or BLE feature along with Classic Bluetooth. ESP32 BLE is aimed at battery operated, low-power wireless communication between different BLE Devices. We will learn all the essentials in order to use BLE in ESP32 and configure it as […]

The post How to use BLE in ESP32? ESP32 BLE (Bluetooth Low Energy) Tutorial appeared first on ElectronicsHub.

]]>
In this tutorial, we will learn about the BLE feature in ESP32. ESP32 has Bluetooth low Energy or BLE feature along with Classic Bluetooth. ESP32 BLE is aimed at battery operated, low-power wireless communication between different BLE Devices. We will learn all the essentials in order to use BLE in ESP32 and configure it as both a Server and a Client. We will also see how can we connect a smart phone with ESP32 BLE Server.

ESP32-BLE-Image-1

A Brief Note on BLE (Bluetooth Low Energy)

The main feature of the Bluetooth 4.0 Specification was the integration of Bluetooth Low Energy or simply BLE. BLE (previously marketed as Bluetooth Smart) is a highly optimized version of Classic Bluetooth designed specifically for low power wireless communication.

While the Classic Bluetooth (often described as Basic Rate and Enhanced Dara Rate in technical documents referring to its data speeds) was designed as a replacement to wired communication with short-range wireless communication. It was designed with data speeds in mind and the applications include large file transfers, wireless audio, etc.

BLE on the other hand is optimized for low power consumption instead of higher data rates and is designed with IoT and battery-operated applications in mind. Some of the popular applications of BLE are smart watches, fitness trackers, health monitoring devices, radio beacons, etc.

BLE-Applications

Unlike Classic Bluetooth, which is always connected, the BLE is usually in standby (idle) mode and wakes up if and only if required. Hence, the ultra-low power consumption.

Bluetooth Low Energy also works on the same 2.4 GHz ISM frequency band. What this means is that a single antenna can be used for Wi-Fi and both the versions of Bluetooth.

Different Layers in BLE

A BLE device consists of three layers:

  • Controller
  • Host
  • Application

BLE-Layers

The controller contains the following:

  • PHY – The Physical Layer
  • LL – The Link Layer
  • HCI – Controller side Host-Controller Interface

The host contains the following:

  • HCI – Host side Host-Controller Interface
  • L2CAP – Logical Link Control and Application Protocol
  • SM – Security Manager
  • ATT – Attribute Protocol
  • GAP – Generic Access Profile
  • GATT – Generic Attribute Profile

The application layer is the highest layer which contains user interface, data handling and logical aspects of the application.

Important Terms in BLE

Let us briefly see some of the important terms associated with BLE.

  • GATT: It is short for Generic Attribute Profile. It defines the specifications for data transfer between BLE devices using Service and Characteristics.
  • Characteristic: Characteristic is a group of information called Attribute and Attribute is a group of information transferred between devices. A characteristic usually contains the following attributes:
  • Value: Data value of the characteristic
  • Declaration: Properties of the characteristic (location, type like read, write, notify, indicate etc.)
  • Description: ASCII String describing the characteristic.
  • Service: A collection of characteristics is called a Service. Each Service has a unique 16-bit or 128-bit ID called UUID.
  • UUID: Universally Unique Identifier is a 128-bit ID given to each service and characteristic in a profile. Use the website UUIDGenerator to generate unique IDs. Each service and characteristic has a unique 16-bit or 128-bit ID called UUID. A sample UUID looks something like this:
    •  583f8b30-74b4-4757-8143-56048fd88b25 

BLE States

A BLE device can have 5 possible states:

  • Standby
  • Advertising
  • Scanning
  • Initiating
  • Connected

BLE-Device-States

BLE Network Topologies

The communication between two BLE devices can be either a Broadcast Type or a Connection Type. In broadcasting, the ‘broadcaster’ BLE Device sends data to any ‘observer’ BLE Device. It is a one-way data transfer.

For two-way communication, you need a ‘Connection’ between the BLE Devices. A Central (Master) BLE Device repeatedly scans for advertising data packets from Peripheral (Slave) BLE Device which sends the packets.

BLE in ESP32

We know that the main feature of ESP32 is Wi-Fi. But additionally, the ESP32 SoC also has Bluetooth support as well. ESP32 Bluetooth is a dual-mode system. This means ESP32 supports both Classic Bluetooth as well as Bluetooth Low Energy (BLE).

Specifically, the Bluetooth system in ESP32 is compliant with Bluetooth v4.2 Classic Bluetooth (BR/EDR) and BLE specifications. In the previous tutorial, we already explored how to use the ESP32 Classic Bluetooth with a couple of examples. We will explore the BLE feature of ESP32 in this tutorial.

ESP32 BLE Server Client Model

Any BLE Device can be configured as a Server or a Client. ESP32 is no exception to this i.e., ESP32 can be either a Server, which advertises its presence so that clients can read its data or as a Client, which scans for servers and makes a connection to receive data from the server.

In this project, we will use two ESP32 Boards, one programmed as BLE Server and the other as BLE Slave.[ESP32 Projects for Beginners]

ESP32 BLE Server

First, we will see how to setup an ESP32 as a BLE Server. Take an ESP32 Board and connect it to the computer. We will call this board as ‘ESP32-BLE_Server’. Open Arduino IDE and make sure that proper ESP32 Development Board and COM port is selected.

Code

This is a slightly modified code of the ‘BLE_server’ example. I commented the code to explain all the necessary things.

Upload this code to the Server ESP32 Device.

ESP32 BLE Client

Take another ESP32 Board (which we will call ESP32-BLE_Client) and connect it to the computer. In the Arduino IDE, select the COM port for this board.

Code

There is an example called ‘BLE_client’ as a part of the ESP32 BLE Library. I used the same code here (with small modifications). Upload this code to Client ESP32 Device.

Working

After uploading codes to both the ESP32 Boards, you need to monitor the serial ports of both the boards to see the communication. For this, I am using the Arduino IDE’s Serial Monitor to open the COM port of the ESP32 BLE Client Device and another software (like Putty or Terminal) to open the serial port of ESP32 BLE Server Device.

After opening the respective serial port terminals for both ESP32 BLE Server and Client, reset both the boards by pressing respective ENABLE (EN) buttons.

ESP32-BLE-Tutorial-Working

The ESP32 BLE Server device will start the BLE Server and starts advertising its services. If you observe the Client ESP32’s Code, we used the Server ESP32’s Service UUID and Characteristic UUID. What this means is that the Client ESP32 scans for BLE devices and if the BLE with the particular UUIDs is found, then it makes a connection.

Once the connection is established, the ESP32 BLE Client first reads the characteristic value from the server (we set this as ‘Hello, World!’ in the Server’s code) and prints it on the terminal.

After that, the client tries to set the characteristic value of the server with a new value every couple of seconds. You can see the client’s terminal in the following image.

Coming to the Server, once it starts the BLE Server, it reads its own characteristic value every couple of seconds. You can see the following image that the Server’s characteristic value is updated by the client and is read by the server.

ESP32-BLE-Server-Terminal

Testing ESP32 BLE Server with Smart Phone

Let us now see how can we connect our smart phones to an ESP32 BLE Server. Upload the following code to the ESP32 Board, which we will call ESP32 BLE Server and open the serial monitor in Arduino IDE.

In the smart phone, go to play store and install ‘BLE Scanner’ application by Bluepixel Technologies. After installing, open the app (if ESP32 Bluetooth is already paired before, then there is no need pair once again). Enable Bluetooth and Location (both are needed for scanning BLE devices) in the phone (the app will prompt the same). Click on the ‘scan’ icon on the top right corner.

The app will start scanning for BLE Devices and lists them out. You can see ‘ESP32-BLE-Server’ in the list.

BLE-Scanner-1

Click on ‘CONNECT’ and smart phone starts connection with ESP32 BLE Server. Once connected, it will display the list of services and its characteristics. Since we have initialized ESP32 with only one service, you can see the Service UUID and also the Characteristic UUID under the ‘CUSTOM SERVICE’ section.

BLE-Scanner-2

To read the initial characteristic value of the service, tap on the ‘R’ icon in the ‘CUSTOM SERVICE’ section. The app reads the value from the server and displays it in the ‘Value’ option.

BLE-Scanner-3

Since we set the properties of the characteristic as both read and write, we can set the value of the characteristic just by tapping on the ‘W’ icon. A text box pops up to write the value. After entering the value, click on ‘OK’. The value is set in Server.

BLE-Scanner-4

If you open the Serial Monitor of the Server, you can see the server prints out newly set value. Instead of continuously monitoring the ‘value’ field like we did in the previous example, we are actually using the callback feature of BLE Library, which will be called if and only if a ‘write’ option is initiated.

BLE-Scanner-5

Conclusion

A complete beginner’s guide on understanding BLE in ESP32. You learn some basic features of Bluetooth Low Energy, how to use ESP32 BLE feature, how to setup ESP32 as BLE Server and Client and also how to connect a smart phone to ESP32 BLE Server.

This is just an introductory project with regards to ESP32 Bluetooth Low Energy. In the future, we will make use of BLE in ESP32 to implement several IoT related applications.

The post How to use BLE in ESP32? ESP32 BLE (Bluetooth Low Energy) Tutorial appeared first on ElectronicsHub.

]]>
https://www.electronicshub.org/esp32-ble-tutorial/feed/ 3
How to Setup Raspberry Pi Pico Serial Programming? USB Serial output https://www.electronicshub.org/raspberry-pi-pico-serial-programming/ https://www.electronicshub.org/raspberry-pi-pico-serial-programming/#comments Mon, 22 Mar 2021 13:10:58 +0000 https://www.electronicshub.org/?p=2034646 In this tutorial, we will learn how to use the Serial Port of Raspberry Pi Pico and display some text. This tutorial is applicable only if you are programming your Raspberry Pi Pico in C using the official SDK. We will learn all the necessary steps for Raspberry Pi Pico Serial Programming and print ‘Hello, […]

The post How to Setup Raspberry Pi Pico Serial Programming? USB Serial output appeared first on ElectronicsHub.

]]>
In this tutorial, we will learn how to use the Serial Port of Raspberry Pi Pico and display some text. This tutorial is applicable only if you are programming your Raspberry Pi Pico in C using the official SDK. We will learn all the necessary steps for Raspberry Pi Pico Serial Programming and print ‘Hello, World!’ text on the Serial Output.

Raspberry-Pi-Pico

A Brief Note on Serial Port of Raspberry Pi Pico

After Blinking an LED using Raspberry Pi Pico, the next logical step is to use the serial port and print some text like Hello World, for example. The important point here is not the text that is printed through the serial port but the process involved for Raspberry Pi Pico to transfer data to the serial port.

The beautiful thing about the Raspberry Pi Pico Serial C SDK is that it defines the ‘stdio’ style functions like ‘printf’ for us to use.

Before proceeding with programming Raspberry Pi Pico Serial Port, we have to understand a little bit about Raspberry Pi Pico’s Serial input and output. The serial input and output of Raspberry Pi Pico can be either UART or USB CDC.

Raspberry Pi Pico has two UART peripherals: UART0 and UART1. When you use the ‘printf’ function to print data, the default serial port is UART and in that UART0 peripheral is used.

NOTE: Pico SDK has a board configuration file called ‘pico.h’, which sets some of the important pins as default for different operations like UART, on-board LED, I2C, SPI etc.

According to this board configuration file, UART0 is the default UART Peripheral and the default UART0_TX Pin is GPIO 0 (GP0) and UART0_RX Pin is GPIO 1 (GP1). Take a look at the pinout of Raspberry Pi Pico in the ‘Getting Started with Raspberry Pi Pico’ tutorial for all the possible GPIO Pins that can be configured as UART0 or UART1 pins.

Exploring Hello World Example

With the brief introduction to Raspberry Pi Pico’s Serial Programming, let us no proceed to explore the examples for serial output. Before proceeding further, if you haven’t gone through the initial setup for Programming Raspberry Pi Pico with C using C SDK, then I strongly suggest you to refer to the previous tutorial called ‘Programming Raspberry Pi Pico with C’.

In that tutorial, I discussed all the necessary steps for setting up your Raspberry Pi (this is Raspberry Pi Computer not the Pico) like downloading the tools, SDK, examples and programming Pico with a Blinky example.

In the ‘pico-examples’ directory (which is in /home/pi/pico), you can see the list of all the examples organized into several directories. In the previous tutorial, we explored the ‘blink’ directory (and the corresponding source files).

cd ~
cd pico/pico-examples
ls

If you go through the list of directories, there is a directory called ‘hello_world’. This directory contains the example projects for serial port programming.

Raspberry-Pi-Pico-Serial-1

Change to ‘hello_world’ directory and list out its contents. You can see it contains two directories called ‘serial’ and ‘usb’. If you want serial communication through UART Port, then use the project in the ‘serial’ directory. If you want to use the USB Serial option of Raspberry Pi Pico, then go to ‘usb’ directory.

cd hello_world
ls

Raspberry-Pi-Pico-Serial-2

First, we will see the project in the ‘serial’ directory, which when executed, prints the text ‘Hello, world!’ through UART of Raspberry Pi Pico. Change to ‘serial’ directory and list out the contents. You can see two files in this directory called ‘CMakeLists.txt’ and ‘hello_serial.c’.

cd serial
ls

Raspberry-Pi-Pico-Serial-3

To view the source code for ‘hello_serial.c’, you can use vim with the help of the following command.

vim hello_serial.c

For your reference, I copied the code below.

NOTE: To exit from vim, type ‘:q’.

The code is very simple. After some hardware initialization (for UART or USB), the program prints ‘Hello, world!’ text continuously with a delay of 1000 ms. If you notice the code, there is nothing that specifies whether to use UART or USB. The reason for this it is configured in CMake.

Using CMake directives, we can set the output of serial to either UART or USB CDC (or both). In fact, if you open the ‘usb’ directory in the ‘hello_world’ directory, you can see two files: ‘CMakeLists.txt’ and ‘hello_usb.c’. The contents of ‘hello_usb.c’ are exactly the same as that of ‘hello_serial.c’.

The difference lies in the corresponding ‘CMakeLists.txt’ file in each directory. Without even modifying the source code, you can essentially enable UART or USB as serial output just by configuring the ‘CMakeLists.txt’ file.

Exploring CMakeLists File

In the ‘serial’ directory of ‘hello_world’ directory, open the ‘CMakeLists.txt’ file using vim. I copied the contents of ‘CMakeLists.txt’ for your reference.

Since the default configuration in the SDK is to use the UART0 as the serial port, there is nothing significant in this ‘CMakeLists.txt’ file. If you want to see the actual CMake directive which enables and/or disables UART and USB, then we have to take a look at the ‘CMakeLists.txt’ file in the ‘usb’ directory of ‘hello_world’ directory.

Assuming you are in ‘serial’ directory of ‘hello_world’ directory, use the following commands to go to ‘usb’ directory and open the ‘CMakeLists.txt’ file in the ‘usb’ directory.

cd ..
cd usb
ls
vim CMakeLists.txt

Raspberry-Pi-Pico-Serial-4

I copied the contents of ‘CMakeLists.txt’ in ‘usb’ directory for your reference.

There are two lines in this file which help in setting the output of serial to UART or USB. They are:

pico_enable_stdio_usb(hello_usb 1)
pico_enable_stdio_uart(hello_usb 0)

These lines will enable printf output via USB and disable printf output via UART.

NOTE: In the future projects, if you want to enable ‘printf’ output via USB, then you have to include these lines in the corresponding ‘CMakeLists.txt’ file of that project.

Building Hello World Example

Now that we have seen how to configure the CMake to set the output as either UART or USB, let us proceed with building the projects in the ‘hello_world’ directory. If you remember the ‘blink’ project, the build process is same.

Go to ‘build’ directory in ‘pico-examples’ directory and in that change to ‘hello_world’ directory.

cd ~
cd pico/pico-examples
ls
cd build
ls
cd hello_world

Raspberry-Pi-Pico-Serial-5

Start the build process using make command. Optionally, you can also specify the number of parallel jobs using -j option.

make -j4

Raspberry-Pi-Pico-Serial-6

It will take few seconds to finish the build. This will build both the examples in the ‘hello_world’ directory ‘serial’ and ‘usb’.

Raspberry-Pi-Pico-Serial-7

To view the list of generated files, change to individual project’s build directory. For example, let us see the files generated for ‘usb’ example.

ls
cd usb
ls

Raspberry-Pi-Pico-Serial-8

You can see the following four target files for ‘usb’ example project:

  • hello_usb.bin
  • hello_usb.elf
  • hello_usb.hex
  • hello_usb.uf2

Similarly, the ‘serial’ project also consists of four target files in the ‘/pico/pico-examples/build/hello_world/serial’ directory.

  • hello_serial.bin
  • hello_serial.elf
  • hello_serial.hex
  • hello_serial.uf2

Flashing Raspberry Pi Pico

We will be using USB example as it is more convenient to view the serial output. I already discussed how to upload the program to Raspberry Pi Pico in the previous tutorials. But as a reminder, I will briefly go through the steps once again.

Hold the ‘BOOTSEL’ button while plugging in the Raspberry Pi Pico to the host computer (Raspberry Pi in my case). This will make the Raspberry Pi Pico to appear as a removable storage device called RPI-RP2.

I also explained a simple trick to add a push button to reset Raspberry Pi Pico, which eliminates the need for unplugging and re-plugging every time you want upload a program.

Browse to the Hello World USB project build directory (home/pi/pico/pico-examples/builds/hello_world/usb) and drag-and-drop the ‘hello_usb.uf2’ file into the RPI-RP2 device. Raspberry Pi Pico reboots and starts executing the code.

Raspberry-Pi-Pico-Serial-9

How to See USB Output?

We know that after flashing Raspberry Pi Pico, it started running the program. But how to view the output of the USB? We have to use a special tool called ‘Minicom’. To install ‘minicom’, use the following command in the terminal.

sudo apt install minicom

Raspberry-Pi-Pico-Serial-10

My computer (Raspberry Pi) already has minicom installed.

To open the serial port, type the following command in the terminal:

minicom -b 115200 -o -D /dev/ttyACM0

As soon as you hit enter, minicom opens the serial port with baud rate of 115200 and you can see ‘Hello, world!’ being continuously printed.

Raspberry-Pi-Pico-Serial-11

To exit minicom, press Ctrl+a and then x.

NOTE: I haven’t discussed how to view the output through UART of Raspberry Pi Pico as using USB is very easy. But if you are interested in that as well, then I will update this tutorial with a new section on serial output on Raspberry Pi Pico’s UART.

Conclusion

A complete tutorial on Raspberry Pi Pico Serial Port Programming in explained here. You learned how to configure Raspberry Pi Pico to generate a serial output, how to use CMake to direct the serial output to either UART or USB, install necessary tools and finally view the printf output printed through USB.

The post How to Setup Raspberry Pi Pico Serial Programming? USB Serial output appeared first on ElectronicsHub.

]]>
https://www.electronicshub.org/raspberry-pi-pico-serial-programming/feed/ 1