Raspberry Pi Pico W

Controlling RGB LED with Raspberry Pi Pico W Web Server using MicroPython

Introduction

Controlling RGB LED with Raspberry Pi Pico W Web Server using MicroPythonin this article, I will teach you how to create a web page to control the RGB LED over wifi using Raspberry Pi Pico W, and I  will also teach you how to set up the local network web server on Raspberry PI  Pico W. so this article will be very helpful to you. as you know the main thing is the internet while make the web-based projects. So, if you’re not familiar with how to connect to Wi-Fi, I recommend you to read my previous article to learn about Wi-Fi connectivity.




Project Overview:

In this project, i will show you how to connect a Raspberry Pi Pico W to a Wi-Fi network and use its onboard LED to indicate the network connectivity status. When the onboard LED blinks it denotes that the Wi-Fi connection is not established. Once the blinking stops it indicates that the Pico W has successfully connected to the Wi-Fi network. Additionally, we will set up a web server on the Raspberry Pi Pico W accessible via a local network IP address through any internet browser.

A web control page will be designed using HTML and CSS. This webpage will feature text view widgets that will display the values of sliders in RGB format, and will also show the names of the sliders, for example, Red, Green, and Blue. To control RGB LED, we will use 3 sliders. Along with this, we will use a button that will send the slider values to the Raspberry Pi Pico W through a Web Server, and the RGB LED will display the light based on the values set on the slider. Initially, we will test this project on a desktop, but later, we will also access the same local network IP address from our mobile to control the RGB LED through the web server using our mobile browser.

Amazon links:

Raspberry Pi Pico W

RGB LED Module

Resistor

Breadboard

Micro USB Cable

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!



Raspberry Pi Pico W Overview:

RGB LED with Raspberry Pi Pico W

RP2040 Chip Details

At the heart of the Raspberry Pi Pico W lies the RP2040, a custom ARM chip crafted by the Raspberry Foundation. Featuring a dual-core ARM Cortex M0+ processor, it boasts flexibility and power with a clock speed of up to 133 MHz. This positions the Pico W as an effective and adaptable choice for diverse projects.

Wireless Features

The standout feature of the Pico W is its wireless functionality, enabled by the Infineon CYW43439 wireless chip. Offering IEEE 802.11 b/g/n wireless LAN and Bluetooth 5.2 capabilities, it propels the board into a new era of connected and Internet of Things (IoT) applications.

GPIOs and Compatibility

The Raspberry Pi Pico W comes equipped with 40 pins, including 26 versatile GPIOs, all adhering to a 3.3V logic level. Designed for pin-to-pin compatibility with the earlier Pico model, it ensures seamless transition and convenience for upgrades in existing projects. Additionally, it features a 3-Pin header for debugging near the RP2040 Chip, enhancing its functionality.



Raspberry Pi Pico W Specifications:

Specification Detail
Microcontroller Chip RP2040 (by Raspberry Pi)
Processor Dual-core ARM Cortex M0+
Clock Speed Up to 133 MHz
SRAM 264kB
Flash Memory 2MB onboard
Wireless Interfaces Single-band 2.4GHz (802.11n)
Bluetooth Bluetooth 5.2
Wireless Chip Infineon CYW43439
GPIO Pins 26 multi-function GPIO pins
Interfaces 2×SPI, 2×I2C, 2×UART
Analog-to-Digital Converters 3×12-bit ADC
PWM Channels 16×controllable
USB Support USB 1.1 Host and Device
Power Modes Low-power sleep and dormant modes
Additional Features Temperature sensor, floating-point libraries
Programmable IO 8×Programmable IO (PIO) state machines
PCB Dimensions 51mm x 21mm x 1mm
Power Input USB port or VSYS pin
Programming Languages C/C++ and MicroPython
IDE Support Thonny IDE, Arduino IDE

RGB LED Module:

An RGB LED module is an electronic component that combines red, green, and blue (RGB) light-emitting diodes (LEDs) in a single package. By mixing these three colors in different intensities, it can produce a wide spectrum of colors. This versatility makes RGB LEDs popular in various applications, from decorative lighting to display technology.

RGB LED with Raspberry Pi Pico W



Pinout of a Typical RGB LED  Module:

RGB LED with Raspberry Pi Pico W

VCC (Power): This is the positive supply voltage pin. It’s typically connected to a power source (like a battery or a power supply unit). The voltage requirements can vary depending on the type of RGB LED, but common values are 5V or 12V.

Red (R), Green (G), and Blue (B) Pins: These are the control pins for each color. By adjusting the voltage or PWM (pulse-width modulation) signal applied to these pins, you can control the intensity of each color. Lowering the voltage or duty cycle dims the light, and increasing it makes the light brighter.

GND (Ground): This is the negative supply voltage pin. It’s connected to the ground of the power source. In some RGB LEDs, especially common anode types, this pin might be labeled as “Common Anode” and connected to VCC instead of ground.

Types of RGB LEDs:

Common Anode: In this type, all the anodes of the LEDs are connected to a common pin (usually the VCC). The individual colors are controlled by connecting the RGB pins to ground, often through a resistor or a microcontroller.

Common Cathode: Here, all the cathodes are connected to a common pin (usually the GND). The colors are controlled by supplying voltage to the RGB pins.




RGB LED Module with Raspberry Pi Pico W Circuit diagram:

RGB LED with Raspberry Pi Pico W

As you can see, the circuit is very simple. I have connected the GND pin of the RGB LED module to the GND of the Raspberry Pi Pico W, the blue pin of the RGB LED module to the GPIO16 of the Raspberry Pi Pico W, the green pin of the RGB LED module to the GPIO17 of the Raspberry Pi Pico W, and the Red pin of the RGB LED Module to the GPIO18 of the Raspberry Pi Pico W.

RGB LED with Raspberry Pi Pico W Circuit diagram:

RGB LED with Raspberry Pi Pico W

If you’re not using an RGB LED module, then please refer to this circuit diagram. The connections remain the same, except that a 330-ohm resistor is used for current limiting between the LED and the Raspberry Pi Pico W.



Controlling RGB LED with Raspberry Pi Pico Web Server Program:



File saving in the Raspberry Pi Pico W:

Open your preferred IDE, for example, Thonny IDE or any other IDE you choose. Copy and paste the above program into the IDE.

Then, go to the ‘File‘ menu and select ‘Save‘.

RGB LED with Raspberry Pi Pico W

A dialog box titled “Where to Save To” will appear. Here, choose the Raspberry Pi Pico as the destination.

RGB LED with Raspberry Pi Pico W

Then Name the file ‘main.py’ and click the ‘OK‘ button to save it.

RGB LED with Raspberry Pi Pico W

Once the program saved, just press the ‘Run‘ button or the ‘F5‘ button on the keyboard.

RGB LED with Raspberry Pi Pico W

After clicking the Run button, you will be able to see the following output in console.

RGB LED with Raspberry Pi Pico W

Simply copy the local IP address and paste it into any web browser. However, ensure that your device is connected to the same network.

RGB LED with Raspberry Pi Pico W

After pasting the IP address into your browser, you will see the page design as follows.

RGB LED with Raspberry Pi Pico W



Test on Desktop

As you move the sliders, you will observe the changes.

RGB LED with Raspberry Pi Pico W

RGB LED with Raspberry Pi Pico W

RGB LED with Raspberry Pi Pico W

Test on Mobile:

Access the same IP address on mobile

RGB LED with Raspberry Pi Pico W

RGB LED with Raspberry Pi Pico W

RGB LED with Raspberry Pi Pico W




Code Explanation:

This section imports the necessary modules and sets up the Wi-Fi credentials. It activates the Wi-Fi interface in station mode and attempts to connect to the specified Wi-Fi network using the given SSID and password.

This line sets up the onboard LED of the Raspberry Pi Pico W to indicate Wi-Fi connection status.

Here, the code configures the pins connected to the RGB LED as PWM (Pulse Width Modulation) outputs, allowing for the control of brightness for each color component. It also sets a frequency for the PWM signal.

Two functions are defined: set_rgb_color to change the color of the RGB LED by adjusting the PWM duty cycle, and blink_led to blink the onboard LED as a status indicator.



This loop attempts to connect to Wi-Fi a specified number of times, blinking the status LED with each attempt.

The code sets up a simple web server by binding a socket to all available IP addresses on port 80 (HTTP) and listening for incoming connections.

A multiline string html contains the HTML and CSS code for the web page, which will be used to control the RGB LED from a web browser.



This function is designed to handle incoming client requests, parse the query parameters, and update the RGB LED color accordingly.

The main loop continuously checks if the board is still connected to Wi-Fi and listens for incoming client connections to handle them.

A try-except block is used to gracefully handle socket timeouts without stopping the program.

 

 

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button