Neostatic

pico W

With 2MB of flash memory, 26 gpio pins, 2.4GHz WiFi, Bluetooth 5.2, operating from 1.8 to 5 volts, the pico W can for a lot for twenty bucks!

Automation in the palm of your hand. With a nearby wifi connection its easy to make any relay wifi controlled. Or connect the pico to a real-time-clock and trigger a relay at a specific time of day or week or month or year!

Posted by Jim, on 11/26/2025


blink!

To blink an led with micropython first import a few things like Timer and Pin from machine.

The pico has pin's that you must assign as an input or output. In this case we are assigning the variable led25 as an output pin and using pin "LED" which is a special pin that means the onboard led. Other pins are usually just numbers.

Then create a timer function, define it and use the pin variable from before with .toggle()

Lastly initialize timer with some information. Im using 0.5 to toggle every half second.

  1. from machine import Pin, Timer
  2. # Onboard LED is connected to GPIO pin 25 on Pico W
  3. led = Pin("LED", Pin.OUT)
  4. # Define a timer callback to toggle LED
  5. def blink(timer):
  6. led.toggle()
  7. # Blink every 500ms (0.5 seconds)
  8. timer = Timer()
  9. timer.init(freq=2, mode=Timer.PERIODIC, callback=blink)

Top


Wifi Connect:

Wifi connection starts with importing network and time. Declaring a pair of variables for network ssid and password. These are the name of your network as it appears to your devices like computer or cell phone. And password.

Declaring station mode, activating, and attempting to connect.

A while loop iterating until a successful connection has been established can be helpful in your project.

  1. import network
  2. import time
  3. ssid = 'YourWiFiSSID'
  4. password = 'YourWiFiPassword'
  5. wlan = network.WLAN(network.STA_IF) # Station mode
  6. wlan.active(True)
  7. wlan.connect(ssid, password)
  8. # Wait for connection
  9. while not wlan.isconnected():
  10. print("Connecting...")
  11. time.sleep(1)
  12. print("Connected! Network config:", wlan.ifconfig())

Top


Wifi LED Control:

This is a fun snippet. Some of the key places you can easily tweak are at line 21, you can customize the static web page your pico will broadcast.

Lines 30 and 34 dirrectly correspond to functions on lines 101 and line 106.

This code can be easily modified to turn on or off gpio pins instead of the LED pin. Add as many web page buttons or functions you want.

With python you can add time delays, turn a pin on or off a specific number of times etc. Sequentially turn pins on or off one at a time, the possibilities are only limited by your imagination when you realise that an output pin can control a relay and a relay can control anything from a lamp, to a buzzer, motor, linear actuator, a low voltage relay can trigger a larger current relay and control a heater, car charger, or any other electrical load.

  1. import network
  2. import socket
  3. import time
  4. import random
  5. from machine import Pin
  6. # Create an LED object on pin 'LED'
  7. led = Pin("LED", Pin.OUT)
  8. # Wi-Fi credentials
  9. ssid = 'ssid_here'
  10. password = 'your_password'
  11. # HTML template for the webpage
  12. def webpage(random_value, state):
  13. html = f"""
  14. <!DOCTYPE html>
  15. <html>
  16. <head>
  17. <title>Pico Web Server</title>
  18. <meta name="viewport" content="width=device-width, initial-scale=1">
  19. </head>
  20. <body>
  21. <h1>Raspberry Pi Pico Web Server</h1>
  22. <h2>Led Control</h2>
  23. <form action="./lighton">
  24. <input type="submit" value="Light on" />
  25. </form>
  26. <br>
  27. <form action="./lightoff">
  28. <input type="submit" value="Light off" />
  29. </form>
  30. <p>LED state: {state}</p>
  31. <h2>Fetch New Value</h2>
  32. <form action="./value">
  33. <input type="submit" value="Fetch value" />
  34. </form>
  35. <p>Fetched value: {random_value}</p>
  36. </body>
  37. </html>
  38. """
  39. return str(html)
  40. # Connect to WLAN
  41. wlan = network.WLAN(network.STA_IF)
  42. wlan.active(True)
  43. wlan.connect(ssid, password)
  44. # Wait for Wi-Fi connection
  45. connection_timeout = 10
  46. while connection_timeout > 0:
  47. if wlan.status() >= 3:
  48. break
  49. connection_timeout -= 1
  50. print('Waiting for Wi-Fi connection...')
  51. time.sleep(1)
  52. # Check if connection is successful
  53. if wlan.status() != 3:
  54. raise RuntimeError('Failed to establish a network connection')
  55. else:
  56. print('Connection successful!')
  57. network_info = wlan.ifconfig()
  58. print('IP address:', network_info[0])
  59. # Set up socket and start listening
  60. addr1 = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
  61. s = socket.socket()
  62. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  63. s.bind(addr1)
  64. s.listen()
  65. print('Listening on', addr1)
  66. # Initialize variables
  67. state = "OFF"
  68. random_value = 0
  69. # Main loop to listen for connections
  70. while True:
  71. try:
  72. conn, addr1 = s.accept()
  73. print('Got a connection from', addr1)
  74. # Receive and parse the request
  75. request = conn.recv(1024)
  76. request = str(request)
  77. print('Request content = %s' % request)
  78. try:
  79. request = request.split()[1]
  80. print('Request:', request)
  81. except IndexError:
  82. pass
  83. # Process the request and update variables
  84. if request == '/lighton?':
  85. print("LED on")
  86. led.value(1)
  87. print("On!")
  88. state = "ON"
  89. elif request == '/lightoff?':
  90. led.value(0)
  91. print("Off!")
  92. state = 'OFF'
  93. elif request == '/value?':
  94. random_value = random.randint(0, 20)
  95. # Generate HTML response
  96. response = webpage(random_value, state)
  97. # Send the HTTP response and close the connection
  98. conn.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
  99. conn.send(response)
  100. conn.close()
  101. except OSError as e:
  102. conn.close()
  103. print('Connection closed')

- Pico W runs a web server on port 80
- When you visit its IP address in a browser, youl see links to turn the LED ON or OFF.
- Clicking those links sends /led/on or /led/off requests, which the Pico W interprets to control the LED.


Thanks for visiting, more updates as i get a chance. 11/26/2025

© 2025 Top