Monthly Archives: November 2020

Arduino Tutorial: Slide Potentiometer (Slide Pot) controls LED strip (WS2812B)

This tutorial explains what slide potentiometers are and teaches how you can use them with an Arduino microcontroller to control something. Here, as an example, the number of LEDs of a WS2812b-based LED strip will be controlled. The example is similar to sound mixers and music decks where sliders are used to control e.g. the volume. Sometimes, they give an additional visual feedback of the slider position by the use of a small bar with LEDs.

Material

– Arduino Uno [Search on Aliexpress | Amazon | eBay.com]
– Slide pot [Search on Aliexpress | Amazon | eBay.com]
– LED strip [Search on Aliexpress | Amazon | eBay.com]
– Jumper Wires [Search on Aliexpress | Amazon | eBay.com]

Components

Slide potentiometer

Slide pot module with two (coupled) outputs (2x DTB).

Slide potentiometers come with many names: potentiometer sliders, volume sliders, mixer, fader, and so on. From a technical perspective, slide potentiometers are just slide knobs that adjust a potentiometer by changing the knobs position. Then, the potentiometer changes a voltage level of the signal that is bound to an output pin. This signal can be used by the analog-to-digital converters of an Arduino (=analog pins) to use the position in an Arduino program.

Slide Pot is the name of a very common module that can easily be used with an Arduino. The typical use case for a slide pot is 1) to position the slide knob, 2) use an Arduino to retrieve a signal that corresponds to the knob position, and then, 3) let the Arduino control something. Typically, the slide pot module comes with a red PCB, a slider/fader knob that adjusts a potentiometer, and pins for two data outputs. The slide pot is available in different variants. My variant has also two data outputs but they are on the same wire. As a result, it is not possible to ‘configure’ two different types of output signals with my variant. This is indicated by the pin labels of my slide pot. Both of my signal pins are labeled with ‘DTB’ (Data B). Other variants have two different labels for the signal pins: ‘DTA’ and ‘DTB’ (Data A and Data B). In this tutorial, we need only one signal from the slider. Therefore, the use of the second signal output is not addressed at all in this tutorial!

LED strip (WS2812B)

WS2812B LED strip (IP67 variant with 30 LEDs per meter).

As the name suggests LED strips / stripes are strips with a specific number of LEDs on them. There exist tons of different types of LED strips. In this tutorial we make use of WS2812B LED strips. These LED strips are fully controllable: Every single LED on the strip can be switched on or off. Moreover, the color is also fully controllable as the WS2812B is a so-called “RGB LED” chip.

I already made a tutorial on how to use WS2812B-based LED strips. With the knowledge you gain by this tutorial, it should be easily possible to control also other LED chips or LED components, such as LED rings. If you plan to use LED rings, have a look at my WS2813 LED ring tutorial.

Part I: Slide Pot Basics

The first part of the tutorial explains some basic aspects about the slide pot. The goal is to retrieve the slide knob position and to print it out on the serial monitor of the Arduino IDE.

An output of the slide pot has three pins: GND, VCC and DTB. GND is connected with ground. VCC is connected to the voltage signal. My slide pot does not indicate which voltage levels are supported on VCC. However, voltage levels of 3.3V and 5V are possible for sure. I would be careful with higher voltage levels. Keep in mind, working with voltage levels of higher than 5V might damage your Arduino permanently if a wrong wiring is applied.

The signal pin DTB has a voltage level that corresponds to the slider knob position and depends on the voltage level of VCC. For example, if VCC has 5V, the voltage levels look like this:

  • DTB = 0V on the most outer left position
  • DTB = 2.5V on the middle position
  • DTB = 5V on the most outer right position

So, the most outer right position is always equal to the voltage level of VCC.

The Arduino’s analog pins are able to read voltage levels up to 5V by a so-called analog-to-digital converter. As a result, the analog value will be available as digital values in the Arduino program. The digital values range from 0 (for 0V at analog pin) to 1023 (for 5V at analog pin).

Wiring

The following wiring picture shows the most basic wiring for the slide pot modules. For GND and VCC, the Arduino’s GND and 5V pins are used. The slide pot’s signal pin is wired to the Arduino’s analog pin A0.

Programming

In the setup function, the serial connection is started to print the values on the serial monitor. Moreover, the analog input A0 is set to input-mode. In the loop function, the current value of slide knob is read, and then, printed out to the serial monitor.

/*
MIT License
Copyright 2020 Michael Schoeffler (https://www.mschoeffler.de)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
 * This is a simple example program on how to use slide pot modules with an Arduino. 
 * The program reads the current slide knob position and prints it out to the serial monitor.
 */

#define PIN_SLIDE_POT_A A0


void setup() {
  Serial.begin(9600);
  pinMode(PIN_SLIDE_POT_A, INPUT );
}

void loop() {
  int value_slide_pot_a = analogRead(PIN_SLIDE_POT_A);
  Serial.print("Slide Pot value: ");
  Serial.println(value_slide_pot_a);
}

Application (VCC = 5V)

If everything is wired correctly and the program has been transferred to the Arduino, the program starts to run and shows the slide knob values on the serial monitor. If the slide knob is on a position within the ‘right area’, the value is around 981 (see previous wiring image for used knob position):

The most outer right position will result in a value of 1023.

Application (VCC = 3.3V)

Let’s change the slide pot’s VCC pin to 3.3V.

As a result, the maximum voltage level for DTB is 3.3V. As the Arduino’s analog-to-digital converter works within the range of 0V to 5V, the same slide knob position will result in a lower value in the Arduino code. If you run the program again with 3.3V on VCC, you will see that the value for the same position is around 654:

With this setup, the most outer right position will result in a value of around 700.

Application (VCC = not connected, pin mode = INPUT_PULLUP)

If you run out of the 3.3V and 5V pins on the Arduino, you can also use the slide pot without connecting VCC to a voltage level:

On programming level, you have to change the pin mode from INPUT to INPUT_PULLUP.

...
pinMode(PIN_SLIDE_POT_A, INPUT_PULLUP);
...

In this mode, an internal pull-up resistor becomes active. As a result, we are still able to get the the knob position. However, as you can see. The resolution becomes even lower than with 3.3V. The same position results in a value of about 133:

With this setup, the most outer right position will result in a value of 134. If you are wondering, why the value for the most outer right position is not higher, the reason is that the values are not linearly distributed. On my setup, if I use the pullup mode, the maximum number is already retrieved, if the knob is moved to the middle. Depending on your use case, you might have to implement some optimizations, if you require a perfectly linear distribution of the value range.

Part II: Controlling the WS2812B LED strip

Next, we will use the knowledge we gathered so far and use it to extend our program to control a WS2812B LED strip.

Wiring

In addition to the previous wiring, we have to wire the LED strip to the Arduino. Three wires are required for the LED strip: GND, +5V, and SIG. As the name of the strip’s ‘+5V’ pin indicates, the LED strip requires 5V. Unfortunately, the Arduino has only a single 5V pin which can either be used for the slide pot or the LED strip. Luckily, we know that the slide Pot can also work with 3.3V or even without a voltage level. Here, we go for the 3.3V alternative and wire the slide Pot’s VCC pin to the Arduino’s 3.3V pin:

Programming

As I wrote already a tutorial about the WS2812B, I will not explain the WS2812B-related lines of code in detail. In order to relate the slide knob position to the number of LEDs to be switched on, it is important to define the maximum analog value (MAX_SLIDE_POT_ANALGOG_READ_VALUE) and number of LEDs (NUM_LEDS). These maximum values define the upper limits of the ‘analog input range’ and ‘LED range’. The remaining part, such as initializing FastLED, is just basic code which can be looked up in my previous tutorial.

In the loop function, three processing steps are executed:

  1. The current slide knob position value is read.
  2. The position value is mapped from the ‘analog input range’ to ‘LED range’. The result of the map function corresponds to the LEDs that must be switched on. If you have never worked before with the map function, have a look at the official documentation.
  3. Switch on LEDs that correspond to the area left of the slide knob, and switch off LEDs that correspond to the area right of the slide knob.

In the following source code, I set the number of LEDs to 10 although my LED strip has more LEDs. The reason is that an LED strip draws a lot of current and the 5V pin is typically limited to 400mA. If you plan to switch on a lot of LEDs, you require an external power supply.

/*
MIT License
Copyright 2020 Michael Schoeffler (https://www.mschoeffler.de)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
 * This is a simple example program on how to use slide pot modules with an Arduino. 
 * In particular, tis programs shows how to control an WS2812B LED strip with a slide pot input.
 */
 
#include "FastLED.h"

#define PIN_SLIDE_POT_A A0 // input pin of the slide pot
#define MAX_SLIDE_POT_ANALGOG_READ_VALUE 700 // maximum voltage as analog-to-digital converted value, depends on the voltage level of the VCC pin. Examples: 5V = 1023; 3.3V ~700

#define NUM_LEDS 10 // add number of LEDs of your RGB LED strip
#define PIN_LED 3 // digital output PIN that is connected to DIN of the RGB LED strip
#define LED_COLOR CRGB::DarkOrchid // see https://github.com/FastLED/FastLED/wiki/Pixel-reference for a full list, e.g. CRGB::AliceBlue, CRGB::Amethyst, CRGB::AntiqueWhite...

CRGB rgb_led[NUM_LEDS]; // color array of the LED RGB strip

void setup() {
  Serial.begin(9600);
  
  pinMode(PIN_SLIDE_POT_A, INPUT);
  FastLED.addLeds<WS2812B, PIN_LED>(rgb_led, NUM_LEDS);  
  
  Serial.println("Setup done.");
}

void loop() {
  // 1) Analog value of slide pot is read
  int value_slide_pot_a = analogRead(PIN_SLIDE_POT_A);
  Serial.print("Slide Pot value: ");
  Serial.println(value_slide_pot_a);

  // 2) Analog value is mapped from slide pot range (analog input value) to led range (number of LEDs)
  int num_leds_switchedon = map(value_slide_pot_a, 0, MAX_SLIDE_POT_ANALGOG_READ_VALUE, 0, NUM_LEDS);  


  // 3) Light up the LEDs
  // Only LEDs are switched on which correspond to the area left of the slide knob
  for (int i = 0; i < num_leds_switchedon; ++i) {
    rgb_led[i] = LED_COLOR;
  }  
  // LEDs are switched off which correspond to the area right of the slide knob
  for (int i = num_leds_switchedon; i < NUM_LEDS; ++i) {
    rgb_led[i] = CRGB::Black;
  }
  FastLED.show();
}

Application

If the code is transferred to the Arduino, the LEDs should light up based on the position of the slide knob.

Slide knob at a left-middle position; 3 LEDs are switched on:

Slide knob at the most outer right position; all 10 LEDs are switched on:

Video tutorial

Tutorial: Reyax RYB080I_Lite/RYB080I Bluetooth Module + Serial communication + Arduino Uno + OLED SH 1106

This tutorials shows how to wire the RYB080I bluetooth module to an Arduino Uno. Moreover, an OLED SH1106 mini display is wired to the Arduino. The idea is that messages are received via bluetooth and then displayed on the OLED display. The messages are sent from an ‘bluetooth serial terminal’ smartphone app.

Equipment:
– Reyax RYB080I [Search on Aliexpress | Amazon | eBay.com]
– OLED display SH1106 [Search on Aliexpress | Amazon | eBay.com]
– Arduino Uno [Search on Aliexpress | Amazon | eBay.com]
– Jumper Wires [Search on Aliexpress | Amazon | eBay.com]
– 5V-to-3.3V converter [Search on Aliexpress | Amazon | eBay.com]
– USB-to-TTL adapter (for playing around with the module) [Search on Aliexpress | Amazon | eBay.com]

Reyax RYB080I

Reyax RYB080I Bluetooth module on a breakout board RYB080I_lite.

The RYB080I is a Bluetooth v4.2 + v5.0 BLE module. It comes with a TI CC2640R2F ARM® Cortex®-M3 chip and has a PCB-integrated antenna. The module can be controlled by just sending so-called AT commands. The AT commends are used, e.g. to check the connection status, set the power mode, scan bluetooth devices etc.

My module comes on a ready-to-use breakout board called RYB080I_lite. Fortunately, the module is fully set up for bluetooth connections when starting it for the first time. In my opinion, this is an advantage especially for ‘makers’. The breakout board has five pins for powering and communicating with the module. The following table shows which pins are available including their purpose.

Pin NameInput/OutputDescription
GNDInputGround.
RST InputLow reset reset signal.
TXD OutputUART serial output.
RXD InputUART serial input.
VDDInput supply voltage (1.8-3.8V).
Source: RYB080I_lite Data Sheet (Link to original PDF)

Part I: Playing around with the Reyax RYB080I module

Before, we start with wiring the module to an Arduino Uno, we do some prototyping with the RYB080I module. In order to do this, we require a USB-to-TTL adapter and a terminal program.

USB-to-TTL adapter device in 3.3V mode.

USB-to-TTL adapters enable to build up serial connection from a PC/notebook to modules offering an TXD (transmit) and RXD (receive) pin. Typically, these adapters can also power the modules with 3.3V or 5V. Simply plug the adapter into an USB port of your computer. To wire an adapter to the RYB080I module, you can use (female/female) jumper wires. The following table shows how to wire the USB-to-TTL adapter to the RYB080I module. Notice: The adapter must power the module with 3.3V (instead of 5V) to avoid damaging the module due to high voltages.

USB-to-TTL PinRYWB116_Lite Pin
5Vno connection / yellow jumper with USB-TO-TTL VCC
VCCno connection / yellow jumper with USB-TO-TTL 5V
3V3VDD
TXDRXD
RXDTXD
GNDGND
How to wire a USB-to-TTL device to an RYB080I_lite breakout board to establish a serial connection.
HTerm v.0.8.5 after starting (“no serial connection traffic yet”)

Besides wiring the adapter, we require a “terminal program” on our computer to send commands to the module. In this tutorial, I make use of HTerm which is a free-to-use terminal program. It has many convenience functions which makes it very comfortable when working with serial connections. I won’t introduce HTerm in this tutorial, since we require only some basic functions.

Now, we are fully set to play around a little bit with the RYB080I module. First, we use HTerm to connect to the adapter/module (Baud: 9600, Data 8, Stop 1, Parity None). When sending commands, it is important to end each line with the CR+LF characters. You find a full list of commands in the data sheet. As a start, you can send the “AT” command to test whether the module is able to receive commands. If the module received the command, it will respond with “+OK”:

< AT\r\n
> +OK\r\n
On this screenshot, an AT command has successfully been sent to the module. The module responses with “+OK”.

Btw. sometimes the module does not react to a sent command, even if wired correctly. Just resend the command a few times. Normally, after 2-3 tries, it should send a response. Ok, let’s ask the module about its MAC address by sending the “AT+ADDR?” command. As a response, you should receive the modules MAC address (attention: the MAC address of you module will be different from mine!):

< AT+ADDR?\r\n
> +ADDR=A434F1A5C026\r\n

Next, use a smartphone app to connect to the RYB080I and send some messages. If we stay connected in HTerm, we will see the message that are transmitted via bluetooth. The module is already fully set up to start bluetooth connections. There exist multiple apps that can start bluetooth connections with the module. Since I have an Android phone, I used Serial Bluetooth Terminal which is free-to-use. The app is simple to use, just go to devices and look for “REYAX_BLE_RYB080I” (default bluetooth boradcast name of the module). If you touch the name, you get connected to the module. In my setup, it was required to use a custom bluetooth LE profile (the app gives you some guidance). When you established the connection, you can send messages from the app to the module. If performed successfully, the bluetooth message will appear in HTerm:

“Hello World!” has been entered into the app. The message was sent via bluetooth to the RYB080I module. The module forwards the message to the serial connection output (shown in HTerm).

If you completed all steps, you are well prepared for the next step. We use the same mechanism to send messages from the app to the bluetooth module. An Arduino Uno, connected to the module, will show the message on an OLED display.

Part II: Displaying bluetooth message on an OLED display (by utilizing an Arduino Uno)

Wiring

5V to 3.3V converter that I use in this tutorial.

First, we have to do all the wiring: The RYB080I bluetooth module and OLE display have to be wired to the Arduino. The next table shows how the bluetooth module is wired to the Arduino. Important: The bluetooth module’s RXD pin expects a voltage level of 3.3V (instead of Arduino’s 5V level). As a consequence, the Arduino’s digital pin 3 should not directly be wired to the RYB080I’s RXD pin. In such cases, I make use of so-called 5V-to-3.3V converters (come with many names).

RYB080I Pin NameWIRE COLORARDUINO PIN NAME
GNDblackGND
RST – (not used)
TXD blue2
RXD yellow (via 5V-to-3.3V converter module!)3
VDDorange3.3V

Then, the OLED display has to be wired to the Arduino which is not that simply. So far, I made many tutorials in which a display is used, e.g. the tutorial about the KMR 1.8 display. I receive a lot of messages from readers that my code does not work with their setup. Typically, it turns out that they do not use the exact same display type that I’m using. Please keep this in mind, when using my wiring and source code for your project. The display that I’m using in this tutorial is a noname 1.3″ OLED 4-SPI module with seven pins. The following table shows how to wire the display to the Arduino:

1.3″ OLED 4-SPI Pin NameWIRE COLORARDUINO PIN NAME
GNDblackGND
VDDred5V
SCKyellow13
SDAblue11
RESwhite8
DCgreen9
CSgrey10

If everything has been wired correctly, your setup should look like this:

Complete wiring of the tutorial’s setup.

Software / Source code

The source code makes use of the software serial and the 8g2 library. The software serial library is utilized to read all the messages coming from the bluetooth module. The 8g2 library is a library for controlling displays that supports tons of different display types. Luckily, also my display type was supported (U8X8_SH1106_128X64_WINSTAR_4W_SW_SPI).

When the Arduino program starts, the display and software serial is initialized. When done, the display shows the message “I’m ready”. In the Arduino’s loop function, characters are read from the software serial (bluetooth module) and stored to a string buffer. If a line ending (‘\n’) is received, the content of the string buffer is shown on the display.

/*
MIT License
Copyright 2020 Michael Schoeffler (https://www.mschoeffler.de)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
 * This is example code of an tutorial about how to use the RYB080I bluetooth module in combination with an OLED display module. 
 * Messages that are received by the bluetooth module are shown on the display module.
 */

#include <U8x8lib.h>
#include <SoftwareSerial.h>

// OLED PINs
#define OLED_SCK 13
#define OLED_MOSI 11
#define OLED_CS 10
#define OLED_DC 9
#define OLED_RES 8

// Bluetooth module pins
#define BLE_RX 2 
#define BLE_TX 3

U8X8_SH1106_128X64_WINSTAR_4W_SW_SPI display (OLED_SCK, OLED_MOSI, OLED_CS, OLED_DC, OLED_RES); // display
SoftwareSerial sserial =  SoftwareSerial(BLE_RX, BLE_TX); // bluetooth module serial connection

String data; // variable used as buffer for the serial connection


void setup() {
  display.begin(); // Display is now ready to use
  display.setFont(u8x8_font_chroma48medium8_r); // set font type 
  display.clear(); // clear contents of display, set cursor to 0,0
  sserial.begin(9600); // start serial connection to bluetooth module
  display.print("I'm ready!"); // Show message on display  
}

void loop() {   
  while(sserial.available()) {      
    char c = sserial.read();  //gets one byte from serial buffer (bluetooth module)
    if (c != -1) {
      data += c; // store character into buffer string
      if (c == '\n') { // end of line detected
        display.clear(); // clear display
        display.print(data); // show new message on display
        data = ""; // clear buffer string
        break;
     }
    }
  }  
}

If everything is correctly executed, you should be able to send messages from the smartphone app to the module. At the same time, the messages should appear on the OLED display:

A message “mschoeffler.de” is sent from the smartphone app to the bluetooth module / Arduino in order to be shown on the OLED display.

Video tutorial