Raspberry Pi Pico W

Control LED with Raspberry Pi Pico W Web Server

Introduction

Control LED with Raspberry Pi Pico W Web Server – The Raspberry Pi Pico W is a versatile microcontroller board that provides a cost-effective solution for building embedded projects. With its built-in Wi-Fi capabilities, it opens up a world of possibilities for remote control and monitoring applications. In this article, we will explore how to set up a local network web server on the Raspberry Pi Pico W and use it to control LED using MicroPython.

As you know, in a web server-based project, the main requirement is an internet connection. So, if you’re not familiar with how to connect to Wi-Fi, I recommend reading my previous article to learn about Wi-Fi connectivity.




Project Overview:

In this project, we will demonstrate 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 signifies 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 a text view to display feedback about the system’s status and a button to control an external LED. When interacted with, the button on the web page will toggle the state of the LED, and the text view will update to reflect the current status of the LED. This setup allows for a simple yet interactive way to control and monitor the LED remotely through the web interface.



Amazon links:

Raspberry Pi Pico W

LEDs

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:

led and Raspberry pi pico W

The Raspberry Pi Pico W is an advanced version of the original Raspberry Pi Pico microcontroller board, distinguished primarily by its integrated wireless connectivity feature. This board retains all the versatile functionalities of its predecessor, the Raspberry Pi Pico, which is known for its compact design and powerful processing capabilities. However, the Raspberry Pi Pico W goes a step further by incorporating Wi-Fi connectivity, making it ideal for Internet of Things (IoT) projects and applications requiring remote access or network communication. This added wireless feature enhances its usability in various fields, such as home automation, wireless sensor networks, and smart devices, allowing for seamless integration into connected systems and enabling users to develop a wider range of innovative projects and applications.



Raspberry Pi Pico W Specifications:

Specification Details
Microcontroller RP2040
Processor Dual-core ARM Cortex-M0+ processor, up to 133 MHz
RAM 264KB SRAM
On-board Storage 2MB Flash memory
Wireless Connectivity 2.4GHz 802.11n wireless LAN (Wi-Fi)
GPIO Pins 26 multi-function GPIO pins
Analog Inputs 3 analog inputs (12-bit ADC)
UART, SPI, I2C Available
PWM Channels 16
USB 1 x Micro-USB B port (USB 1.1)
Power Supply 5V via USB port or external source
Dimensions 51mm x 21mm
Other Features – Programmable I/O for extended peripheral support- On-board temperature sensor- Real-time clock (RTC) capability

 Circuit Diagram:

led and Raspberry pi pico W

The yellow wire is connected to GPIO15 (General Purpose Input/Output pin 15) on the Raspberry Pi Pico W.

The black wire is connected to a GND (Ground) pin on the Raspberry Pi Pico W.

Between the GPIO15 pin and the LED, there’s a 330ohm resistor. The resistor is there to limit the current flowing through the LED, protecting it from burning out due to excessive current.



Control LED With Raspberry Pi Pico W 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‘.

led and Raspberry pi pico W

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

led and Raspberry pi pico W

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

led and Raspberry pi pico W

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

led and Raspberry pi pico W

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

led and 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.

led and Raspberry pi pico W

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

led and Raspberry pi pico W



Desktop Testing:

led and Raspberry pi pico W

led and Raspberry pi pico W

Mobile Testing:

Access the same ip address on mobile

led and Raspberry pi pico W



Code Explanation:

the code begins with the import network statement, which imports the MicroPython network module, enabling functionalities to connect to Wi-Fi networks. Additionally, import time is utilized to incorporate time-related functions, while import machine grants access to hardware components on the microcontroller board. The ssid and password variables store the SSID and password of the desired Wi-Fi network for connection. The wlan = network.WLAN(network.STA_IF) line initializes a WLAN interface in station mode, allowing the microcontroller to connect to an existing Wi-Fi network. Activating the WLAN interface is achieved with wlan.active(True), and wlan.connect(ssid, password) attempts connection to the specified Wi-Fi network using the provided credentials.

The variable status_led denotes the onboard LED, typically assumed to be connected to the predefine GPIO. Its primary function is to provide visual cues indicating the status of the Wi-Fi connection. On the other hand, led refers to an external LED connected specifically to GPIO pin 15. This LED serves the purpose of being controllable through the web interface, offering users the ability to interact with it remotely.

The function blink_led is established to alternate the state of the onboard LED, represented by status_led, with a delay of 0.5 seconds. This functionality results in a blinking effect, offering visual feedback or indication through the LED’s changing state.

the code endeavors to establish a connection to a Wi-Fi network by executing a loop, attempting connection for a specified maximum number of times (max_attempts). Upon successful connection, indicated by the wlan.isconnected() function returning True, the program prints a success message alongside the network configuration details. Additionally, it sets the status LED to indicate a successful connection. Conversely, if the connection attempt fails, the program prints a failure message and adjusts the status LED accordingly to reflect the unsuccessful connection. This process ensures that the device properly handles both successful and failed Wi-Fi connection attempts, providing appropriate feedback through printed messages and LED status indicators.



The line addr = socket.getaddrinfo(‘0.0.0.0’, 80)[0][-1] retrieves address information required to bind a socket to all accessible network interfaces, specified as ‘0.0.0.0’, on port 80, which is the conventional HTTP port. Following this, s = socket.socket() creates a new socket object named s, while s.bind(addr) binds the socket to the address obtained earlier. Finally, s.listen(1) initiates the socket to start listening for incoming connections, allowing a maximum queue length of 1 connection. This series of operations sets up the foundation for the web server to receive and handle client requests effectively.

This HTML document serves as a user interface for controlling an LED through a web interface. It provides a simple layout with a button to toggle the LED state and a display area to show the current LED state. JavaScript code is included to handle the button click event and send an asynchronous request to the server to toggle the LED state. The server-side code will respond to this request by changing the LED state and updating the HTML content to reflect the new state. Overall, this interface allows users to control an LED remotely using a web browser.




The handle_client function is designed to manage incoming HTTP requests from clients in the server-side code. Upon receiving a request, it first checks whether the request contains the string ‘GET /toggle’, which signifies a request to toggle the LED. If such a request is detected, the function toggles the state of the LED accordingly. Subsequently, it updates the response HTML to reflect the current state of the LED and sends this updated response back to the client, allowing the client-side interface to dynamically reflect the changes made to the LED state. This function facilitates the interaction between the client and server, enabling real-time control of the LED through the web interface.

The main loop of the code continuously monitors the status of the Wi-Fi connection. In case the device gets disconnected from the Wi-Fi network, it initiates a blinking pattern on the onboard LED to visually indicate the disconnection status, providing a quick visual cue to the user. Additionally, the loop listens for incoming client connections. Upon detecting a new client connection, it directs the handling of client requests to the handle_client function, which manages the processing of HTTP requests sent by clients, such as toggling the LED state in response to user input from the web interface. This loop ensures the continuous operation of the server, managing both the Wi-Fi connection status and client interactions seamlessly.



Conclusion

The Raspberry Pi Pico W is a versatile microcontroller board that can be used for a wide range of projects. By setting up a web server on the Pico W, we can easily control LEDs and other components remotely. This article provided a step-by-step guide on how to control LED with Raspberry Pi Pico W web server. Now, it’s time to unleash your creativity and explore the endless possibilities of the Pico W!

 

Related Articles

Leave a Reply

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

Back to top button