Arduino Nano RP2040 Connect

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search

A simple IDE to program MicroPython on the board is the Arduino Lab for MicroPython IDE. Alternatives are, Jupyter Notebook or Thonny IDE.

Upgrade the Arduino Firmware[edit]

  • Reinstall the MicroPython Firmware (version 1.22.2 tested)
  • Install the Arduino IDE
  • Start Arduino IDE and select the Arduino RP2040 board
  • Go to Tools -> Firmware Update -> Check updates and select version 1.5.0 and press Install

Install the MicroPython Firmware[edit]

Note: Before installing MicroPython ensure you have the latest Arduino Firmware, see Upgrade the Arduino Firmware.

  • Plug the board into the USB port on your computer, it appears as a drive in your filesystem

  • Remove the wire between the GND and REC pin (before you copy the file onto the device)
  • Copy the firmware you downloaded on the drive that showed up in your file system

  • Once it is completely copied to the device, unplug the board and plug in again

Pinout[edit]

How to control the Builtin LED MicroPython[edit]

this is GPIO6 (D13 in Arduino)

from machine import Pin
from time import sleep 

# using the internal LED on the Pico - pin 25 
myLED = Pin(6, Pin.OUT)

while True:
      # this switches the LED on for 1 second
      myLED.on()
      sleep(0.1)
      # this switches the LED off for 500 ms
      myLED.off()
      sleep(0.5)

Example: Read Accelerometer and Gyro[edit]

import time
from lsm6dsox import LSM6DSOX

from machine import Pin, I2C
lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12)))

while (True):
    accel_data = lsm.accel()
    print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*accel_data))
    gyro_data = lsm.gyro()
    print('Gyroscope:     x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*gyro_data))
    print("")
    time.sleep_ms(100)


Example of a WIFI-Access Point and Webserver to control the LED[edit]

# Wi-Fi AP Mode Example
#
# This example shows how to use Wi-Fi in Access Point mode.
# this version by Albrecht Schmidt, https://www.sketching-with-hardware.org/wiki/
# based on the following examples:
# https://randomnerdtutorials.com/esp32-esp8266-micropython-web-server/
# https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-python-api


import network, socket, time
from machine import Pin

led = Pin(6, Pin.OUT)              #on board LED

SSID ='Nano_RP2040_Connect_test'   # Network SSID
KEY  ='12345678'                 # Network key (should be 8 chars) - for real use, choose a safe one
HOST = ''
PORT = 80                          # 80 ist the http standard port, can also use non-privileged port e.g. 8080

# Init wlan module and connect to network
wlan = network.WLAN(network.AP_IF)
wlan.active(True)
# it seems in this version the AP mode only supports WEP
wlan.config(essid=SSID, key=KEY, security=wlan.WEP, channel=2)

print("AP mode started. SSID: {} IP: {}".format(SSID, wlan.ifconfig()[0]))


# create the webpage with a button to toggle the LED
def web_page():
  if led.value() == 1:
    led_state="ON"
  else:
    led_state="OFF"
  
  html ="""<html><head>
      <title>Nano RP2040 Connnect Web Server</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" href="data:,">
      </head>
      <body>
        <h1>Nano RP2040 Connnect </1>
        <h2>Web Server Test</h2>
        <p>LED state: <strong>""" + led_state + """</strong></p><p><a href="/?led=on"><button class="button">ON</button></a></p>
        <p><a href="/?led=off"><button class="button button2">OFF</button></a></p>
      </body>
      </html>"""
  return html

# get started with setting up the sever sockedt
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind and listen
server.bind([HOST, PORT])
server.listen(5)

# loop to deal with  http requests
while True:
  conn, addr = server.accept()
  print('Connection from %s' % str(addr))
  request = conn.recv(1024)
  request = str(request)
  print('Request Content = %s' % request)
  # check if the request includes led=on or off
  led_on = request.find('/?led=on')
  led_off = request.find('/?led=off')
  # request is 'GET /?led=on' or 'GET /?led=off' - the string starts at position 6 (counting starts at 0)
  if led_on == 6:
    print('LED ON')
    led.value(1)
  if led_off == 6:
    print('LED OFF')
    led.value(0)
  response = web_page()
  conn.send('HTTP/1.1 200 OK\n')
  conn.send('Content-Type: text/html\n')
  conn.send('Connection: close\n\n')
  conn.send(response)
  conn.close()

External links[edit]