CETECH Posted October 22 Report Posted October 22 Hey there, futureย Bluetooth Jedi! ๐ Welcome to an electrifying project where we combine the magic of Web Bluetooth,ย the flashy brilliance of NeoPixels,ย and the awesome power of the Seeed Studio Xiao nRF52840 Sense.ย By the end of this journey, you'll be able to control a strip of NeoPixels right from your web browser. Yep, no more complicated apps or cablesโjust pure wireless awesomeness. ๐ฎโจ So, grab your soldering iron, a cup of coffee (or tea โ), and letโs light things up!ย ๐ ๏ธ What Youโll Need: Before we dive into code and soldering, let's check if you have all the necessary gadgets to make this magic happen! 1. Seeed Studio Xiao nRF52840 Sense โก Why this?ย Itโs like a pocket-sized superhero! ๐ช It's BLE-capable, has a built-in IMU, microphone, and is powered by the ARM Cortex-M4 processor. Small but mighty! 2. NeoPixel LED Strip ๐ These RGB LEDs are the stars of our show. They can display millions of colors and are individually addressable. Weโll be using them to dazzle our friends (or just our cat ๐ฑ). 3. Web Browser with Bluetooth Support ๐ For this project, we need a web browser that supportsย Web Bluetooth API. Chrome, Edge, or Chromium-based browsers are perfect. Sorry, Firefox lovers... you'll have to switch sides for this one! ๐ 4. Jumper Wires, Soldering Kit, and USB-C Cable ๐ Standard build-essentials. These will help you hook up everything without blowing things up (which weย totallyย donโt want). Get PCBs for Your Projects Manufactured ย You must check outย PCBWAYย for ordering PCBs online for cheap! You get 10 good-quality PCBs manufactured and shipped to your doorstep for cheap. You will also get a discount on shipping on your first order. Upload your Gerber files ontoย PCBWAYย to get them manufactured with good quality and quick turnaround time. PCBWay now could provide a complete product solution, from design to enclosure production. Check out their online Gerber viewer function. With reward points, you can get free stuff from their gift shop. Also, check out this useful blog on PCBWay Plugin for KiCad fromย here. Using this plugin, you can directly order PCBs in just one click after completing your design in KiCad. Step 1: Setting Up Your Xiao nRF52840 Sense ๐ฆ First, we need to make sure your Xiao is ready for action. Time to upload some code! Install the Development Environment: Installย Arduino IDEย from the official website if you already have it. Next, install theย Seeed nRF52ย boards in Arduino: Go toย File > Preferences. Add this URL toย Additional Boards Manager URLs: https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json Now head toย Tools > Board > Boards Manager, search forย Seeed nRF52ย and install the package. ย Install Libraries: Youโll need to grab a couple of libraries to work with NeoPixels and BLE: Adafruit NeoPixel Libraryย (to handle our shiny lights ๐ก) ArduinoBLE Libraryย (for Bluetooth communication) Head toย Tools > Manage Librariesย and search for these to install them. Step 2: Wiring it Up ๐งโ๐ง๐ Alright, it's time to connect the Xiao to the NeoPixel strip. Donโt worry, this part is easier than figuring out which wire your headphones use! ๐ง Connect the NeoPixels to Xiao: Power (VCC): Connect this to theย 3.3Vย pin on the Xiao. Ground (GND): GND to GND (these two are like peanut butter and jelly ๐ฅชโinseparable). Data In (DIN): Hook this up toย Pin D0ย on the Xiao. Everything wired up? Awesome! Now the fun begins. ๐งโโ๏ธโจ Step 3: Code Time! โจ๏ธ๐ป Let's dive into the code that will make your Xiao and NeoPixels dance to your commands (remotely via Bluetooth!). ๐บ๐ Here's the plan: The Xiao will advertise itself as aย Bluetooth device. Your browser (withย Web Bluetooth) will connect to it and control the NeoPixel strip by sending color commands ย Here's the Arduino Sketch: #include <ArduinoBLE.h> #include <Adafruit_NeoPixel.h> #define NEOPIXEL_PIN 0 #define NUM_PIXELS 64 Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); BLEService ledService("19b10000-e8f2-537e-4f6c-d104768a1214"); BLEByteCharacteristic ledCharacteristic("19b10001-e8f2-537e-4f6c-d104768a1214", BLEWrite | BLERead); void setup() { Serial.begin(115200); if (!BLE.begin()) { Serial.println("starting BLE failed!"); while (1); } BLE.setLocalName("XIAO-LED"); BLE.setAdvertisedService(ledService); ledService.addCharacteristic(ledCharacteristic); BLE.addService(ledService); ledCharacteristic.writeValue(0); strip.begin(); strip.show(); BLE.advertise(); Serial.println("Bluetooth device active, waiting for connections..."); } void loop() { BLEDevice central = BLE.central(); if (central) { Serial.print("Connected to central: "); Serial.println(central.address()); while (central.connected()) { if (ledCharacteristic.written()) { uint8_t ledState = ledCharacteristic.value(); if (ledState == 1) { digitalWrite(LED_BUILTIN, HIGH); // Turn LED on for (int i = 0; i < NUM_PIXELS; i++) { strip.setPixelColor(i, strip.Color(255, 255, 255)); strip.show(); } } else { digitalWrite(LED_BUILTIN, LOW); // Turn LED off for (int i = 0; i < NUM_PIXELS; i++) { strip.setPixelColor(i, strip.Color(0, 0, 0)); strip.show(); } } } } Serial.print("Disconnected from central: "); Serial.println(central. Address()); } } What Does This Code Do? ๐ง BLE Advertising: Your Xiao advertises itself as "NeoPixelController" via Bluetooth. BLE Service: We create a custom Bluetooth service that listens for commands (color changes) from a web browser. NeoPixel Control: Based on the color data sent by the browser, the NeoPixel LEDs change colors accordingly. ย Step 4: Build the Web Bluetooth Interface ๐ Now we move to the browser side, creating aย simple webpageย that will scan for your Xiao and send color commands via Bluetooth. Hereโs a basic HTML + JavaScript setup: <!DOCTYPE html> <html> <head> <title>Web Bluetooth LED Control</title> </head> <body> <button id="connect">Connect</button> <button id="on">Turn On</button> <button id="off">Turn Off</button> <script> let ledCharacteristic; document.getElementById('connect').addEventListener('click', async () => { try { const device = await navigator.bluetooth.requestDevice({ filters: [{ name: 'XIAO-LED' }], optionalServices: ['19b10000-e8f2-537e-4f6c-d104768a1214'] }); const server = await device.gatt.connect(); const service = await server.getPrimaryService('19b10000-e8f2-537e-4f6c-d104768a1214'); ledCharacteristic = await service.getCharacteristic('19b10001-e8f2-537e-4f6c-d104768a1214'); console.log('Connected to device'); } catch (error) { console.error('Error:', error); } }); document.getElementById('on').addEventListener('click', async () => { if (ledCharacteristic) { try { await ledCharacteristic.writeValue(Uint8Array.of(1)); console.log('LED turned on'); } catch (error) { console.error('Error turning on LED:', error); } } }); document.getElementById('off').addEventListener('click', async () => { if (ledCharacteristic) { try { await ledCharacteristic.writeValue(Uint8Array.of(0)); console.log('LED turned off'); } catch (error) { console.error('Error turning off LED:', error); } } }); </script> </body> </html> How This Works: The page contains aย buttonย to connect to the Xiao via Bluetooth. Once connected, you can use the on and off buttons to control the LED. ย Step 5: Time to Shine! โจ๐ก Now for the moment of truth: test your project! ๐ Upload the Arduino code to your Xiao nRF52840 Sense. Open the HTML file in aย supported browserย (like Chrome). Click the "Connect to Xiao" button, and select the device from the Bluetooth device list. Slide the color picker, and watch the magic happen! ๐ชโจ Your NeoPixels should change colors as you adjust the slider. Wireless LED control, baby! Step 6: Add Your Own Magic โจ Congrats, you've just controlled a strip of NeoPixels viaย Web Bluetooth! ๐ But why stop there? Here are some ways to level up: Add color animations: How about some rainbow patterns or breathing effects? ๐ IMU Integration: Use the onboard IMU to control the lights based on motion. You can dance and let the LEDs react to your moves! ๐บ Build a Web Dashboard: Make the webpage more interactive by adding buttons for pre-defined light patterns (disco mode, anyone?). Conclusion: The Wireless Wonderland ๐ ย With Web Bluetooth and the Xiao nRF52840, youโve unlocked the secret to controlling LEDs without touching a wire! ๐ฎ Whether you're jazzing up your living room, building smart home gadgets, or just flexing at your next tech meetup, you're officially in control of the light show! ๐จโ๐ป๐ฉโ๐ป So go on, experiment, and make your project shine brighter than a disco ball at a retro party! ๐โจ Happy hacking, and keep shining! ๐ Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.