### Blink NeoPixel with IdeaBoard Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/4.-IdeaCode-Getting-Started This example demonstrates how to control the NeoPixel on an IdeaBoard. Ensure the IdeaBoard library is imported and the board is connected. ```python from ideaboard import IdeaBoard import time ib = IdeaBoard() while True: ib.pixel = (255, 0, 0) # red time.sleep(0.5) ib.pixel = (0, 0, 0) # off time.sleep(0.5) ``` -------------------------------- ### Digital Input Example Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/6.-CircuitPython-Getting-Started Read the state of a digital input pin. This example configures pin IO33 as an input and optionally enables the internal pull-up resistor. It then continuously prints the pin's value (True for high, False for low). ```python import time import board from digitalio import DigitalInOut, Direction, Pull # Digital Output on IO27 my_input = DigitalInOut(board.IO33) # Choose pin 33 my_input.direction = Direction.INPUT # Optional - default is INPUT my_input.pull = Pull.UP # Add the internal PULLUP resistor if needed while True: print(my_input.value) time.sleep(0.5) ``` ```python False True False False False True True ``` -------------------------------- ### Digital Output Example Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/6.-CircuitPython-Getting-Started Control a digital output pin to toggle between high and low states. This example sets pin IO27 as an output and blinks an LED (or similar device) by setting the pin's value to 1 and 0 repeatedly with a 2-second delay. ```python import time import board from digitalio import DigitalInOut, Direction, Pull # Digital Output on IO27 my_output = DigitalInOut(board.IO27) # Choose pin 27 my_output.direction = Direction.OUTPUT # Set the direction to output while True: my_output.value = 1 # Set value 1 (high/on) time.sleep(2) my_output.value = 0 # Set value 0 (low/off) time.sleep(2) ``` -------------------------------- ### HTTP GET Request with JSON Parsing Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/6.-CircuitPython-Getting-Started Connect to Wi-Fi, perform an HTTP GET request to a specified URL, and parse the JSON response. This example retrieves the International Space Station's current location. ```python import socketpool import ssl import wifi import adafruit_requests as requests socket = socketpool.SocketPool(wifi.radio) https = requests.Session(socket, ssl.create_default_context()) print("Connecting...") wifi.radio.connect("ssid", "password") print("Connected to Wifi!") URL = "http://api.open-notify.org/iss-now.json" data = https.get(URL).json() print(data) long = data["iss_position"]["longitude"] lat = data["iss_position"]["latitude"] print(f"The International Space Station is located at {lat}, {long}") ``` ```python Connecting... Connected to Wifi! {'iss_position': {'longitude': '30.5286', 'latitude': '49.6584'}, 'message': 'success', 'timestamp': 1669389718} The International Space Station is located at 49.6584, 30.5286 ``` -------------------------------- ### Analog Input Example Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/6.-CircuitPython-Getting-Started Read analog values from a pin using the `analogio` module. This example connects to pin IO33 and continuously prints its analog value, which ranges from 0 to 65535. Note that certain pins cannot be used for analog input if Wi-Fi is active. ```python import time import board from analogio import AnalogIn # Connect potentiometer to pin 33 analog_in = AnalogIn(board.IO33) while True: # Values are from 0 to 65535 (2 ** 16) print(analog_in.value) time.sleep(0.1) ``` -------------------------------- ### Adafruit IO MQTT Client Example Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/7.-Adafruit-IO This code connects to WiFi, then establishes an MQTT connection to Adafruit IO. It defines callback functions for connection, disconnection, subscription, unsubscription, and message handling. Requires `secrets.py` for credentials. ```python # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time from random import randint import ssl import socketpool import wifi import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT ### WiFi ### # Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and # "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other # source control. # pylint: disable=no-name-in-module,wrong-import-order try: from secrets import secrets except ImportError: print("WiFi secrets are kept in secrets.py, please add them there!") raise # Set your Adafruit IO Username and Key in secrets.py # (visit io.adafruit.com if you need to create an account, # or if you need your Adafruit IO key.) aio_username = secrets["aio_username"] aio_key = secrets["aio_key"] print("Connecting to %s" % secrets["ssid"]) wifi.radio.connect(secrets["ssid"], secrets["password"]) print("Connected to %s!" % secrets["ssid"]) # Define callback functions which will be called when certain events happen. # pylint: disable=unused-argument def connected(client): # Connected function will be called when the client is connected to Adafruit IO. # This is a good place to subscribe to feed changes. The client parameter # passed to this function is the Adafruit IO MQTT client so you can make # calls against it easily. print("Connected to Adafruit IO! Listening for DemoFeed changes...") # Subscribe to changes on a feed named DemoFeed. client.subscribe("DemoFeed") def subscribe(client, userdata, topic, granted_qos): # This method is called when the client subscribes to a new feed. print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) def unsubscribe(client, userdata, topic, pid): # This method is called when the client unsubscribes from a feed. print("Unsubscribed from {0} with PID {1}".format(topic, pid)) # pylint: disable=unused-argument def disconnected(client): # Disconnected function will be called when the client disconnects. print("Disconnected from Adafruit IO!") # pylint: disable=unused-argument def message(client, feed_id, payload): # Message function will be called when a subscribed feed has a new value. # The feed_id parameter identifies the feed, and the payload parameter has # the new value. print("Feed {0} received new value: {1}".format(feed_id, payload)) # Create a socket pool pool = socketpool.SocketPool(wifi.radio) # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=1883, username=secrets["aio_username"], password=secrets["aio_key"], socket_pool=pool, ssl_context=ssl.create_default_context(), ) # Initialize an Adafruit IO MQTT Client io = IO_MQTT(mqtt_client) # Connect the callback methods defined above to Adafruit IO io.on_connect = connected io.on_disconnect = disconnected io.on_subscribe = subscribe io.on_unsubscribe = unsubscribe io.on_message = message # Connect to Adafruit IO print("Connecting to Adafruit IO...") io.connect() ``` -------------------------------- ### Map Range Function Example Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/5.-IdeaBoard-Library-Usage Utilize the `map_range` function to scale a value from one range to another. This example maps a potentiometer's analog reading (0-65535) to a servo's angle range (0-180). ```python import board from ideaboard import IdeaBoard from analogio import AnalogIn ib = IdeaBoard() pot = AnalogIn(board.IO33) servo = ib.Servo(board.IO4) while True: val = pot.value val = ib.map_range(val, 0, 65535, 0, 180) servo.angle = val ``` -------------------------------- ### Plot Sensor Data with IdeaBoard Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/4.-IdeaCode-Getting-Started This example shows how to read analog sensor data and plot it in real-time using IdeaCode's Plotter tab. The data must be printed in 'label:value' format. ```python import board from ideaboard import IdeaBoard import time ib = IdeaBoard() analog_in = ib.AnalogIn(board.IO33) while True: print("light:", analog_in.value) time.sleep(0.1) ``` -------------------------------- ### Adafruit IO Output Log Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/7.-Adafruit-IO Example output log when running the Adafruit IO publishing script. Shows connection status, subscription, and published/received values. ```python >>> %Run -c $EDITOR_CONTENT Connecting to idealab Connected to idealab! Connecting to Adafruit IO... Connected to Adafruit IO! Listening for DemoFeed changes... Subscribed to crcibernetica/f/DemoFeed2 with QOS level 0 Publishing a new message every 10 seconds... Publishing 58 to DemoFeed. Feed DemoFeed2 received new value: 58 Publishing 84 to DemoFeed. Feed DemoFeed2 received new value: 84 ``` -------------------------------- ### Play Music with RTTTL Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/8.-Playing-Music Use the `play` function from the `adafruit_rtttl` library to play a song on a specified pin. Ensure the `adafruit_rtttl.mpy` library is installed. ```python import board from ideaboard import IdeaBoard from adafruit_rtttl import play song = "IronMan:d=4,o=5,b=155:2b4,2d5,4d5,4e5,2e5,8g5,8f#5,8g5,8f#5,8g5,8f#5,4d5,4d5,4e5,2e5" play(board.IO27, song) ``` -------------------------------- ### Publish Data to Adafruit IO Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/7.-Adafruit-IO Continuously publishes random values to 'DemoFeed' every 10 seconds. Requires Adafruit IO setup and the 'io' object to be initialized. ```python last = 0 print("Publishing a new message every 10 seconds...") while True: # Explicitly pump the message loop. io.loop() # Send a new message every 10 seconds. if (time.monotonic() - last) >= 10: value = randint(0, 100) print("Publishing {0} to DemoFeed.".format(value)) io.publish("DemoFeed", value) last = time.monotonic() ``` -------------------------------- ### Receive Data with ESPNOW Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/9.-ESPNOW Install and run this code on the receiving IdeaBoard to listen for and collect incoming ESPNOW packets until an 'end' message is received. ```python import espnow e = espnow.ESPNow() packets = [] while True: if e: packet = e.read() packets.append(packet) if packet.msg == b'end': break print("packets:", f"length={len(packets)}") for packet in packets: print(packet) ``` -------------------------------- ### Send Data with ESPNOW Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/9.-ESPNOW Install this code on the sending IdeaBoard. Replace the placeholder MAC address with the actual receiver's MAC address. ```python import espnow e = espnow.ESPNow() peer = espnow.Peer(mac=b'ªªªªªª') e.peers.append(peer) e.send("Starting...") for i in range(10): e.send(str(i)*20) e.send(b'end') ``` -------------------------------- ### Get ESP32 MAC Address Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/9.-ESPNOW Use this command in the REPL to retrieve the MAC address of the ESP32, which is required for ESPNOW communication. ```python >>> import wifi >>> wifi.radio.mac_address b'\xd4\xd4\xda\x16\xb7\x1c' ``` -------------------------------- ### Initialize IdeaBoard Instance Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/5.-IdeaBoard-Library-Usage Instantiate the IdeaBoard object to begin using its functionalities. This is the first step for any IdeaBoard interaction. ```python from ideaboard import IdeaBoard ib = IdeaBoard() ``` -------------------------------- ### Import and List Board Pins Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/6.-CircuitPython-Getting-Started Import the `board` module to access and list all available pin names on the IdeaBoard. This is useful for identifying which pins to use for various functionalities. ```python import board dir(board) ``` -------------------------------- ### Basic Wi-Fi Connection Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/6.-CircuitPython-Getting-Started Connect to a Wi-Fi network using provided SSID and password. After a successful connection, it prints the assigned IPv4 address. ```python import wifi print("Connecting...") wifi.radio.connect("ssid", "password") print("Connected to Wifi!") ip = wifi.radio.ipv4_address print(f"IP: {ip}") ``` ```python Connecting... Connected to Wifi! IP: 192.168.84.20 ``` -------------------------------- ### Set RGB LED Color and Brightness Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/5.-IdeaBoard-Library-Usage Control the onboard RGB LED by assigning an RGB tuple to the `pixel` property. Adjust brightness using the `brightness` property (0.0 to 1.0). ```python from ideaboard import IdeaBoard ib = IdeaBoard() # Set brightness from 0.0 to 1.0 ib.brightness = 0.2 # colors are RGB (Red, Green, Blue) BLUE = (0,0,255) GREEN = (0,255,0) RED = (255,0,0) PURPLE = (255,0,255) YELLOW = (255,255,0) ib.pixel = BLUE ``` -------------------------------- ### Write Analog Output Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/5.-IdeaBoard-Library-Usage Generate an analog output signal using `AnalogOut(pin)`. The `value` property accepts a 16-bit number (0 to 65535) corresponding to a voltage range of 0-3.3V. Only pins `board.IO25` and `board.IO26` support Analog Out. ```python import time import board from ideaboard import IdeaBoard ib = IdeaBoard() # Note that there are only two pins on the IdeaBoard # that support AnalogOut: board.IO25 and board.IO26 dac = ib.AnalogOut(board.IO26) dac.value = 32768 # 1.60V on pin board.IO26 ``` -------------------------------- ### Cycle RGB LED through Color Wheel Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/5.-IdeaBoard-Library-Usage Use the `arcoiris` property to cycle the onboard RGB LED through a color wheel. Input values range from 0 to 255, with 0 and 255 both representing red. ```python from ideaboard import IdeaBoard from time import sleep ib = IdeaBoard() # ib.acroiris(0-255) creates a color wheel # from 0 (RED) to 255 (RED) while True: for i in range(256): ib.arcoiris = i sleep(0.01) ``` -------------------------------- ### Adafruit IO Secrets Configuration Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/7.-Adafruit-IO Store WiFi credentials and Adafruit IO API keys in this file. Ensure it is not committed to version control to protect sensitive information. ```python # This file is where you keep secret settings, passwords, and tokens! # If you put them in the code you risk committing that info or sharing it secrets = { 'ssid' : 'ssid', 'password' : 'password', 'aio_username' : 'adafruit_io_username', 'aio_key' : 'adafruit_io_key' } ``` -------------------------------- ### Read Digital Input Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/5.-IdeaBoard-Library-Usage Read digital input signals using `DigitalIn(pin, pullmode)`. The `pull` argument can be `ib.UP` or `ib.DOWN` to set the pull-up resistor. The current state is available via the `value` property. ```python import board import time from ideaboard import IdeaBoard ib = IdeaBoard() #Digital In # entrada = ib.DigitalIn(board.IO27, pull=ib.UP) # pull can be ib.UP or ib.DOWN, default None) entrada = ib.DigitalIn(board.IO27) while True: print(entrada.value) time.sleep(0.5) ``` -------------------------------- ### Write Digital Output Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/5.-IdeaBoard-Library-Usage Control digital output pins by creating a `DigitalOut(pin)` object and setting its `value` property to a truthy or falsy value (e.g., `True`, `False`, `1`, `0`). ```python import board import time from ideaboard import IdeaBoard ib = IdeaBoard() #Digital Out salida = ib.DigitalOut(board.IO27) while True: salida.value = True time.sleep(0.5) salida.value = False time.sleep(0.5) ``` -------------------------------- ### Read Analog Input Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/5.-IdeaBoard-Library-Usage Read analog input signals using `AnalogIn(pin)`. The `value` property returns a 16-bit number (0 to 65535) representing the analog voltage. ```python import board import time from ideaboard import IdeaBoard ib = IdeaBoard() #Analog In entrada = ib.AnalogIn(board.IO33) while True: print(entrada.value) time.sleep(0.5) ``` -------------------------------- ### Control Motor Speed Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/5.-IdeaBoard-Library-Usage Set motor speeds using the `throttle` property, ranging from -1.0 (full reverse) to 1.0 (full forward). A throttle of 0 brakes the motor, and None allows it to roll freely. ```python from ideaboard import IdeaBoard ib = IdeaBoard() # motor speed is from -1.0 (reverse) to 1.0 (forward) # 0 is stopped with brake, None = roll freely ib.motor_1.throttle = 1.0 ib.motor_2.throttle = -1.0 ``` -------------------------------- ### I2C Bus Scan Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/6.-CircuitPython-Getting-Started Connect to the I2C bus and scan for connected devices. This code initializes the I2C bus, locks it for exclusive access, and then iterates through the scanned devices, printing their addresses in hexadecimal format. ```python import board i2c = board.I2C() # Ensure exlusive access to i2c bus while not i2c.try_lock(): pass # scan the bus for device in i2c.scan(): print(hex(device)) ``` ```python 0x10 ``` -------------------------------- ### Control Servo Motor Angle Source: https://github.com/crcibernetica/circuitpython-ideaboard/wiki/5.-IdeaBoard-Library-Usage Control servo motors by creating a `Servo` object with a specified pin and then setting its `angle` property between 0 and 180 degrees. ```python import board from ideaboard import IdeaBoard from time import sleep ib = IdeaBoard() servo1 = ib.Servo(board.IO4) while True: # Set servo angle to 10 degrees servo1.angle = 10 sleep(2) # Set servo angle to 170 degrees servo1.angle = 170 sleep(2) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.