Tuesday, 28 April 2020

Addressable LED Ring controlled by Bluetooth

Need:
Arduino UNO
HC-05 BT module
LED ring 16 addressable lamps
Connecting cables

Make these connections:

VCC on HC-05 to 3.5V on Arduino
GND on HC-05 to GND on Arduino
TX on HC-05 to ~10 on Arduino
RX on HC-05 to ~11 on Arduino

GND on LED ring to GND on Arduino
VCC on LED ring to 5V on Arduino
DIN on LED ring to ~6 on Arduino

Upload this code to the Arduino

#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

SoftwareSerial BT(10, 11);  // TX, RX of the Bluetooth Module

#define PIXEL_PIN      6    // Data Pin of Led strip
#define PIXEL_COUNT    16   // Number of LEDs in the strip
#define BRIGHTNESS   96   // use 96 for medium brightness
#define SPEED          50    // Speed of each Color Transition (in ms)
#define IMMEDIATELY    0    // Transition happen instantly
#define RAINBOW_SPEED  50    // Rainbow Transition speed


Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool offOld = LOW;
bool WhiteOld = LOW;
bool RedOld = LOW;
bool GreenOld = LOW;
bool BlueOld = LOW;
bool TopazOld = LOW;
bool LilacOld = LOW;
bool RainbowOld = LOW;
bool rgbOld = LOW;
int  showType = 0;

void setup() {

   BT.begin(9600);
 
   BT.println("Connected to Arduino");
   

  strip.setBrightness(BRIGHTNESS); 
 

  strip.begin();
  strip.show();
}

char a;

void loop() {
  bool off = LOW;
  bool White = LOW;
  bool Blue = LOW;
  bool Red = LOW;
  bool Green = LOW;
  bool Topaz = LOW;
  bool Lilac = LOW;
  bool Rainbow = LOW;
  bool rgb = LOW;
  bool ende;
   if (BT.available())
   {
    a= (char)BT.read();




    if(a=='o')
    {
      off = HIGH;
          BT.println("TURNING OFF LEDs..");

    }else{
          off = LOW;
    }
   
// ===========================================================================================

    if(a=='w')
    {
      White = HIGH;
          BT.println("TURNING LEDs WHITE");

    }else{
          White = LOW;
    }
   
// ===========================================================================================

    if(a=='b')
    {
      Blue = HIGH;
          BT.println("CHANGING TO BLUE");     
         
    }else{
          Blue = LOW; 
    }

// ===========================================================================================

if(a=='r')
    {
      Red = HIGH;
          BT.println("CHANGING TO RED");           
    }else{
          Red = LOW; 
    }

// ===========================================================================================

if(a=='g')
    {
      Green = HIGH;
          BT.println("CHANGING TO GREEN");     
         
    }else{
          Green = LOW;
    }

// ===========================================================================================

if(a=='t')
    {
      Topaz = HIGH;
          BT.println("CHANGING TO TOPAZ");     
         
    }else{
          Topaz = LOW;
    }

// ===========================================================================================

if(a=='l')
    {
      Lilac = HIGH;
          BT.println("CHANGING TO LILAC");     
         
    }else{
          Lilac = LOW;
    }

// ===========================================================================================

    if(a=='a')
    {
      Rainbow = HIGH;
          BT.println("RAINBOW ANIMATION");     
         
    }else{
          Rainbow = LOW; 
    }
   
// ===========================================================================================

     if(a=='m')
    {
      rgb = HIGH;
          BT.println("MIX COLORS");     
         
    }else{
          rgb = LOW; 
    }
}
if (off == LOW && offOld == HIGH) {
    delay(20);
 
   
    if (off == LOW) {
       showType = 0  ;                            // Off animation Type 0
   
      startShow(showType);
    }
  }

// ===========================================================================================

if (White == LOW && WhiteOld == HIGH) {
    delay(20);
 
   
    if (White == LOW) {
       showType = 1  ;                            // White animation Type 1
   
      startShow(showType);
    }
  }

 
// =========================================================================================== 
 
  if (Red == LOW && RedOld == HIGH) {
    delay(20);
 
   
    if (Red == LOW) {
       showType = 2  ;                            // Red animation Type 2
   
      startShow(showType);
    }
  }

// ===========================================================================================

if (Green == LOW && GreenOld == HIGH) {
    delay(20);
 
   
    if (Green == LOW) {
       showType = 3  ;                            // Green animation Type 3
   
      startShow(showType);
    }
  }
 
// ===========================================================================================

if (Blue == LOW && BlueOld == HIGH) {
    delay(20);
 
   
    if (Blue == LOW) {
       showType = 4  ;                            // Blue animation Type 4
   
      startShow(showType);
    }
  }

// ===========================================================================================

if (Topaz == LOW && TopazOld == HIGH) {
    delay(20);
 
   
    if (Topaz == LOW) {
       showType = 5  ;                            // Topaz animation Type 5
   
      startShow(showType);
    }
  }

// ===========================================================================================

if (Lilac == LOW && LilacOld == HIGH) {
    delay(20);
 
   
    if (Lilac == LOW) {
       showType = 6  ;                            // Topaz animation Type 6
   
      startShow(showType);
    }
  }
   
// ===========================================================================================


    if (Rainbow == LOW && RainbowOld == HIGH) {
    delay(20);

    if (Rainbow == LOW) {
showType = 8;                            // Rainbow animation Type 8
      startShow(showType);
    }
  }

// ===========================================================================================

      if (rgb == LOW && rgbOld == HIGH) {
    delay(20);

    if (rgb == LOW) {
   showType = 7;                            // Mix animation Type 7
     rgb = HIGH;

      startShow(showType);
    }
  }

WhiteOld = White;
RedOld = Red;
BlueOld = Blue;
GreenOld = Green;
TopazOld = Topaz;
LilacOld = Lilac;
offOld = off;
RainbowOld = Rainbow;
rgbOld = rgb;

}


void startShow(int i) {
  switch(i){

    case 0: colorWipe(strip.Color(0, 0, 0), SPEED);    // Black/off
            break;

    case 1: strip.setBrightness(255);                            // Changes the Brightness to MAX
            colorWipe(strip.Color(255, 255, 255), IMMEDIATELY);  // White
            strip.setBrightness(BRIGHTNESS);                     // Reset the Brightness to Default value
            break; 

    case 2: colorWipe(strip.Color(255, 0, 0), SPEED);  // Red
            break;

    case 3: colorWipe(strip.Color(0, 255, 0), SPEED);  // Green
            break;

    case 4: colorWipe(strip.Color(0, 0, 255), SPEED);  // Blue
            break;

    case 5: colorWipe(strip.Color(0, 250, 255), SPEED);  // Topaz
            break;           

    case 6: colorWipe(strip.Color(221, 130, 255), SPEED);  // Lilac
            break;           
   
    case 7: colorWipe(strip.Color(255, 0, 0), SPEED);  // Red
            colorWipe(strip.Color(0, 255, 0), SPEED);  // Green
            colorWipe(strip.Color(0, 0, 255), SPEED);  // Blue
            theaterChase(strip.Color(  0,   0, 127), SPEED); // Blue
            theaterChase(strip.Color(127,   0,   0), SPEED); // Red
            theaterChase(strip.Color(0,   127,   0), SPEED); // Green
            break;

    case 8: rainbowCycle(25);
            break;
  }
}
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*10; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Friday, 27 December 2019

Stranger Things Message Wall Arduino project

I made a version of the message wall seen in the Netflix series 'Stranger Things'. Winona Ryder's character Joyce Byers receives messages from 'the Upside Down'. She hung Christmas lights on her living room wall and the lights lit up next to corresponding letters. The project was a modified version of Zach Hipps' YouTube version and the instructions at his website. I am indebted to him for the Arduino code and the .apk for the BT app.















I wanted to make a smaller version as Christmas gifts for my two daughters. I settled on a 12" x 12" box frame version.

Things you need for the project:

(a) WS2811 Addressable RGB LEDs. I got mine from an eBay seller. You need 28 but they come in strings of 50. There's about 10cm of wire between each LED. When you get them check the wiring diagram carefully. The associated diagrams I got with my lights were wrong. This took a while to figure out as I'll explain later.


(b) Arduino Uno. You can use an original or a clone.

(c) HC-05 Bluetooth Module.

(d) Jumper cables.

(e) 400 point bread board.

(f) 5V 1A power supply.

(g) Soldering iron and solder.

(h) Printout of the picture or a drawing/painting done by a willing accomplice. I used a 12" x 12" piece of scrap-booking wallpaper with a suitable retro rose pattern. This was then over-painted with an approximation of the strings of Christmas fairy lights.

(i) 12" x 12" (300mm x 300mm) box frames. You may have to modify these to get an adequate depth. I attached 6cm wide strips of 5mm MDF to each side. I filled any gaps with wood filler, rubbed them down with sandpaper and painted them.


Instructions.

(a) Cut a length of LEDs, for this project you need 28. There is one LED for each letter A to Z and two needed for blanks to match the bulbs seen in the TV show.

This is a good moment to check the three wires. They may well be different colours from those specified in the instructions received from the supplier. Identify the +5V cable, the GND cable and the DATA cable. If you look closely at each LED they are labelled. Also note the direction arrow on the DATA cable. If you wire them up in the wrong direction they won't work. They won't be damaged they just won't light up.

(b) Check that the LEDs work by running a program from the Fast LED library. See my blog post here.

(c) You may now now want to change a few settings on your HC-05 Bluetooth module. I was making two of these projects so I named one module 'STMW1' and 'STMW2'. Passcodes were changed from the default '1234' to '11' (obviously!) Instructions for changing AT command can be found in my blog post here.

(d) Load up the project Arduino code provided in the introduction to your Arduino. Remove the USB cable.

(e) Now start making the following wire connections. Do them in this order:

GND wire from LED string to GND on Arduino
DATA wire from LED string to Pin 5 on Arduino
+5V wire from LED string to 5V on Arduino.

TX on HC-05 to RX on Arduino
RX on HC-05 to TX on Arduino
GND on HC-05 to GND on Arduino
VCC om HC-05 to +3.3V on Arduino

(f) Power up the Arduino using a 5V 1A adaptor. The HC-05 Bluetooth module should be flashing fairly rapidly.

(g) Download the Android app. It's not available from the Google play store. You need to copy it into an appropriate file destination on your smartphone, then install it. You may have to look up how to do this for your own phone.

(h) Switch Bluetooth on on your smartphone. Pair up with the HC-05 module.

(i) Using the Android app send a message. Watch as each light lights up in sequence. If you try a sentence with spaces in such as 'help me', there should be a slightly longer pause in between the words.

(j) Fix your completed 12" x 12" 'picture' into your adapted box frame. Hot glue the Arduino and the HC-05 onto the back of the frame and box in using a piece of MDF cut to size. Screw down the back plate.

There's a video of the completed project here: https://www.youtube.com/watch?v=NrRv_ULy_Kk






Wednesday, 18 September 2019

How to run Fast LED programs on addressable LEDs using Arduino

Fast LED is a library of programs for controlling addressable LEDs. Before using the libraries or working on writing your own code you need to know what LEDs you are using and how many of them will there be. Make a note of what pin, or pins will you be putting the LEDs onto.

Download the latest release of the Arduino IDE for your operating system. You may already have this.

Open Arduino.

In the 'Tools' menu navigate to 'Manage Libraries'. In the 'Filter your search' box type 'Fast LED'. You should get something like this:



Click on 'install', wait until it installs then from 'File' 'Examples' scroll down and 'Fast LED' should be down towards the bottom of the list. You can then select one of the Fast LED libraries for example 'ColorPalette'. Select this and the code opens in Arduino.

Among the first few lines are the ones to alter depending on your requirements:

#define LED_PIN     5
#define NUM_LEDS    50
#define BRIGHTNESS  64

#define LED_TYPE    WS2811

Change these to match the string of LEDs you are using. 

In my video example below, I'm using Pin 5, 22 LEDs, 100 brightness and a string of WS2811 addressable LEDs.




Wednesday, 4 September 2019

How to control LED via Bluetooth and Arduino

I will cover how to set up Arduino, upload the code, test in serial mode, then test using Android app.

Things required:

1. Arduino Uno board
2. Jumper wires
3. HC-05
4. LED


1. Make the following connections:

Arduino                              HC-05
TX              ====>             TX
RX              ====>             RX
5V              ====>             VCC
GND           ====>             GND

LED short wire ====>       GND of Arduino
LED long wire ====>         pin 8 of Arduino

2. Temporarily remove the TX and RX from the HC-05.



3. Upload the following code:

------------------------------------------------------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
 pinMode(8, OUTPUT); // put your setup code here, to run once:
 }

void loop() {
  // put your main code here, to run repeatedly:
 if(Serial.available()>0)
   {     
      char data= Serial.read(); // reading the data received from the bluetooth module
      switch(data)
      {
        case 'a': digitalWrite(8, HIGH);break; // when a is pressed on the app on your smart phone
        case 'b': digitalWrite(8, LOW);break; // when b is pressed on the app on your smart phone
        default : break;
      }
      Serial.println(data);
   }
   delay(50);

}
------------------------------------------------------------------------------------------------------------------------------------
4. Replace the TX and RX cables as above.

5. Open serial monitor in Arduino program.

6. Change Baud rate to 9600 and select 'no line ending'.

7. Enter a to turn LED on, enter b to turn LED off.


Using via Bluetooth app. I use Bluetooth Terminal

8. Swap the TX and RX cables around on the Arduino.

9. Pair up using Bluetooth app.

10. Enter a in app to turn LED on, enter b in app to turn LED off.

There is a video showing this in action here:


How to enter AT mode in HC-05 Bluetooth module

I will cover how to change password, name or mode (master or slave) in HC-05 Bluetooth module. 


Things required:

1. Arduino Uno board
2. Jumper wires
3. HC-05


1. Upload a blank program to your Arduino.

2. Make the following connections:

Arduino                              HC-05
TX              ====>             TX
RX              ====>             RX
VCC            ====>             VCC
GND           ====>              GND
HC-05 bluetooth module
HC-05


3. Before powering up the Arduino, the little button on the HC-05 should be pressed and then released a few seconds after powering up the Arduino..  

4. The HC-05 in now in AT mode and its LED will blink approximately once every 2 seconds.

5. Open serial monitor in the Arduino program. The Baud rate should be selected to 38400 and both nl and cr should be selected.

6. Check some AT commands:

Type AT and press enter
The response should be OK

AT+ROLE? and press enter
The response is 0 or 1
0- Slave
1- Master

AT+UART? and press enter
The response is Baud rate

AT+PSWD? and press enter
The response is 1234    // that is the password.

For changing the password, command is AT+PSWD="1587"  
This command will set the password to 1587
Note: you need the quotation marks around the number selected.

There are other AT commands available at this link: