Jump to content
Electronics-Lab.com Community

Recommended Posts

Posted

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

2_sS1dUXE2bz.PNG?auto=compress%2Cformat&
ย 

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.
image_k6F1eh7zdh.png?auto=compress%2Cfor
ย 

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.
image_fIosjcjLQ3.png?auto=compress%2Cfor
ย 

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.
image_qNNWiOg5uu.png?auto=compress%2Cfor
ย 

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 ๐ŸŽ†

screenshot_2024-09-14_224931_XD0Bzp4wrQ.
ย 

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! ๐ŸŒŸ


Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
  • Create New...