Building a Raspberry Pi Pico W and LM35 Web Server project

 Building a Raspberry Pi Pico W and LM35 Web Server project involves setting up the Raspberry Pi Pico W to read temperature data from the LM35 sensor and serve this data on a web page. The web server will allow users to access real-time temperature data via their browser.

Here’s a step-by-step guide to help you set up the project:

What You'll Need:

  1. Raspberry Pi Pico W (with Wi-Fi capability)
  2. LM35 Temperature Sensor
  3. Breadboard and Jumper wires
  4. Micro-USB Cable (to power the Pico W and upload the code)
  5. A computer with Thonny or another Python IDE
  6. MicroPython installed on the Raspberry Pi Pico W

Step 1: Setting Up the Raspberry Pi Pico W

Before starting, ensure that MicroPython is installed on the Raspberry Pi Pico W:

  1. Download and Install MicroPython:

    • Visit Raspberry Pi's official site to get the latest firmware for the Raspberry Pi Pico W.
    • Put the Raspberry Pi Pico W in boot mode by holding down the BOOTSEL button and connecting it to your computer.
    • Copy the micropython.uf2 file to the Pico W storage.
  2. Connect the Raspberry Pi Pico W to Wi-Fi:

    • Use MicroPython’s wifi library or another method to connect to a Wi-Fi network.

Step 2: Wiring the LM35 to the Raspberry Pi Pico W

The LM35 has three pins:

  • VCC (Pin 1): Connect this to the 3.3V pin on the Raspberry Pi Pico W.
  • GND (Pin 2): Connect this to a GND pin on the Pico W.
  • Vout (Pin 3): This pin outputs the analog voltage corresponding to the temperature. Connect this to an ADC pin on the Raspberry Pi Pico W (e.g., GPIO 26).

Step 3: Write the Python Code for the Web Server

Use Thonny or another Python IDE to write the code. The code will:

  1. Connect the Pico W to the Wi-Fi network.
  2. Read the temperature from the LM35 sensor.
  3. Serve the temperature data over HTTP using a basic web server.

Here’s an example code to get you started:

Copy Python Code

Python Code Example with Copy Button

import network
import socket
import time
from machine import ADC, Pin

# Connect to Wi-Fi
ssid = "your_SSID"
password = "your_password"

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

# Wait for connection
while not wlan.isconnected():
    time.sleep(0.1)

print("Connected to Wi-Fi!")
print("IP Address:", wlan.ifconfig()[0])

# Set up LM35 on ADC pin (e.g., GPIO26)
lm35 = ADC(Pin(26))

# Function to read temperature in Celsius
def read_temperature():
    # Convert ADC value to voltage (range is 0-3.3V)
    voltage = lm35.read_u16() * 3.3 / 65535
    # LM35 gives 10mV per degree Celsius, so we multiply the voltage by 100 to get the temperature
    temperature = voltage * 100
    return temperature

# Set up the web server
def serve_webpage():
    addr = socket.getaddrinfo(wlan.ifconfig()[0], 80)[0][-1]
    s = socket.socket()
    s.bind(addr)
    s.listen(1)

    print('Listening on', addr)

    while True:
        cl, addr = s.accept()
        print('Client connected from', addr)
        cl.recv(1024)  # Receive HTTP request (not used)
        temperature = read_temperature()
        response = """
        
            Temperature Reading
            
                

Temperature Reading

The current temperature is: {:.2f} °C

""".format(temperature) cl.send('HTTP/1.1 200 OK\r\n') cl.send('Content-Type: text/html\r\n') cl.send('\r\n') cl.send(response) cl.close() # Start the web server serve_webpage()

Step 4: Running the Code

  1. Upload the Code: Upload the code to your Raspberry Pi Pico W using Thonny or any Python IDE that supports MicroPython.
  2. Run the Code: Once the code is running, the Pico W will connect to your Wi-Fi and start a simple web server.
  3. Access the Web Server: Open your browser and enter the IP address of your Raspberry Pi Pico W (which will be shown in the console after the connection). You should see the temperature reading from the LM35 displayed on the webpage.


Step 4: Running the Code

  1. Upload the Code: Upload the Python code to your Raspberry Pi Pico W using Thonny or any other IDE that supports MicroPython.
  2. Run the Code: Once the code is running, the Pico W will connect to the Wi-Fi network and start a web server.
  3. Access the Web Server: Open a web browser and enter the IP address of the Raspberry Pi Pico W (printed in the console after the connection). You should see the temperature reading from the LM35 sensor displayed on the web page.

Step 5: Test the Project

Once the web server is running, you can access the temperature data from any device connected to the same Wi-Fi network. Every time you refresh the web page, it will display the current temperature reading from the LM35 sensor.

Troubleshooting Tips:

  • Ensure that your Wi-Fi credentials (SSID and password) are correctly entered.
  • Check that the LM35 sensor is correctly wired to the Pico W and that the ADC pin is configured properly.
  • If the web server is not loading, check that the IP address is correct and the Pico W is connected to the network.

Expanding the Project

  • Add Data Logging: You can log temperature data to a cloud service or store it locally on the Raspberry Pi Pico W’s filesystem.
  • Improve the UI: Enhance the web page with styling (CSS) or add JavaScript for real-time updates without refreshing.
  • Use MQTT: Instead of a simple HTTP server, you can implement MQTT to send temperature data to a broker for more advanced IoT applications.

This project is a great way to learn about web servers, sensors, and Raspberry Pi Pico W programming. Happy building!

Comments

Popular posts from this blog

Tutorial: Using Raspberry Pi as an OpenVPN Client Router with Ethernet Sharing

How to Use a Raspberry Pi 4 as a Firewall to Block Adult Content and Protect Your Family

Tutorial: Using Raspberry Pi Pico and HW-488 Infrared Obstacle Avoidance Sensor to Play/Pause a Video on Your PC via USB