Tag: Arduino script

Sending motion data to the Blynk app (part 2 of the Blynk tutorial)

In the first blog post we explained the basics of controlling the body interaction 2 (BI2) vibrator development board using the concept of  (virtual) pins. This time we want to send data from the BI2 board to the Blynk app. The BI2 has the MPU-9250 9DoF (9 Degrees of Freedom) IMU (Inertial Measurement Unit) sensor on board. This sensor is a combination of an accelerometer, gyroscope and magnetometer. Especially the accelerometer is important for motion detection. This could be used for controlling the vibrator as show with the body interaction 1 (BI1).

For measuring the motion data we use the asukiaa library. Please search and install the library in the Arduino library manager.

In the program code the library must be included and a MPU9250 sensor object must be defined. Finally we need several variables of the type float.

#include <MPU9250_asukiaaa.h>
MPU9250 mySensor;
float aX, aY, aZ, mDirection, pitch, roll, yaw;

In the setup part of the program we need to tell the MPU9250 how it is connected to the ESP8266 microcontroller. [The MPU9250 IMU is connected by the I2C bus to the ESP8266 microcontroller: the sda pin of the IMU is connected to pin 4, the scl pin to pin 5. The connection between MPU9250 and ESP8266 is managed with the standard Wire library.]

For using the accelerometer and magnetometer we have to initialize the sensor with a begiAccel() call to the IMU library.

Wire.begin(4, 5); //sda, scl
mySensor.setWire(&Wire);
mySensor.beginAccel();
mySensor.beginMag();

We have to tell the program how often data is sent to the app. Therefore we need an important concept in microcontroller programming:

Timer

With the help of the timer we can tell the microcontroller to do a given tasks again and again e.g. after 1000 microsecond. You cannot use the delay function to pass time as this would interrupt the important call to the Blynk.run(); function which is located in the loop part of the program.

First we have to define an object of type Timer.

BlynkTimer timer;

In the setup part we have to say how often what the timer has to do. in this example the timer will call the function myTimerEvent every 1000 microsecond.

timer.setInterval(1000L, myTimerEvent);

In the loop part of the program we have to call the timer to keep things going:

timer.run(); // Initiates BlynkTimer

Now we need the function myTimerEvent what has to be done every 1000 seconds.

void myTimerEvent()
{
  // here add was has to be done
}

First we have to update the sensors (accelUpdate, magUpdate). Then we read out the acceleration data in the X, Y and direction. You can already use this data but they are hard to catch. Therefore we can calculate the pitch, roll and yaw. These are angles from -180° to +180°. The calculation is complicated and I don’t understand it. But with the given formulas you get a very rough approximation which makes the data quite accessible.

void myTimerEvent() {
  mySensor.accelUpdate();
  aX = mySensor.accelX();
  aY = mySensor.accelY();
  aZ = mySensor.accelZ();

  // calculate pitch, roll, yaw (raw approximation)
  float pitch = 180 * atan (aX/sqrt(aY*aY + aZ*aZ))/M_PI;
  float roll = 180 * atan (aY/sqrt(aX*aX + aZ*aZ))/M_PI;
  float yaw = 180 * atan (aZ/sqrt(aX*aX + aZ*aZ))/M_PI;

  // read gyroscope update
  mySensor.magUpdate();
  mDirection = mySensor.magHorizDirection();
}

Finally we send the data back to the Blynk app. Now we use the virtual pins. For the variables pitch we  use virtual pin 2 (V2), for roll V3, for yaw V4 and for mDirection V5. We have to add the following line to the myTimerEvent function.

void myTimerEvent() {
  // send data to app via virtual ports, e.g. virtual pin V2 is set to pitch
  Blynk.virtualWrite(V2, pitch);
  Blynk.virtualWrite(V3, roll);
  Blynk.virtualWrite(V4, yaw);
  Blynk.virtualWrite(V5, mDirection);
}

Now the data are continously sent to the Blynk app. To visualize the data we add the widget SuperChart.

For each variable we have to define the input (virtual) pin. For pitch we use the virtual pin V2. In addition we define the color and style of the graph and more.

 

Finally the super graph shows us the date from the accelerometer which are updated every second.

First part of the tutorial (setup Arduino, setup Blynk, LED and motor control) is here

Here is the complete code:

 

/*************************************************************
bodyinteraction.org
sample program for reading MPU data, setting LED color and motor speed
*/
#define BLYNK_PRINT Serial
// include this library in the Arduino library manager
#include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 1
// LED data pin is connected to pin?
#define DATA_PIN 14

// Define the array of leds
CRGB leds[NUM_LEDS];
int wave;

// include this library in the Arduino library manager
#include <MPU9250_asukiaaa.h>
MPU9250 mySensor;
float aX, aY, aZ, mDirection, pitch, roll, yaw;

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Your Auth Token XXXXXXXXXX";

// Your WiFi credentials.
char ssid[] = "YOUR SSID   XXXXXXXXXXXXXX";
char pass[] = "YOUR Password XXXXXXXXXXXX";
BlynkTimer timer;

void myTimerEvent()
{
  // read acceleration data
  mySensor.accelUpdate();
  aX = mySensor.accelX();
  aY = mySensor.accelY();
  aZ = mySensor.accelZ();
  // read gyroscope update
  mySensor.magUpdate();
  mDirection = mySensor.magHorizDirection();
  // calculate pitch, roll, yaw (raw approximation)
  float pitch = 180 * atan (aX/sqrt(aY*aY + aZ*aZ))/M_PI;
  float roll = 180 * atan (aY/sqrt(aX*aX + aZ*aZ))/M_PI;
  float yaw = 180 * atan (aZ/sqrt(aX*aX + aZ*aZ))/M_PI;
  // send data to app via virtual ports, e.g. virtual pin V2 is set to pitch
  Blynk.virtualWrite(V2, pitch);
  Blynk.virtualWrite(V3, roll);
  Blynk.virtualWrite(V4, yaw);
  Blynk.virtualWrite(V5, mDirection);
}

BLYNK_WRITE(V0) // set RGB color values which are transmitted from the app as V0 (virtual pin 0)
{ 
  int i = param[0].asInt();
  int j = param[1].asInt();
  int k = param[2].asInt();
  leds[0].setRGB(j,i,k);
  FastLED.show();
}

void setup()
{
  Serial.begin(115200);
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

  Wire.begin(4, 5); //sda, scl
  mySensor.setWire(&Wire);
  mySensor.beginAccel();
  mySensor.beginMag();

  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

 

Please feel free to comment or write to jacardano@gmail.com

Blynk: Controlling BI2 from the smartphone

Readers ask me for an easy way to control the body interaction vibrator development board. Without or with limited  programming knowledge, without complicated Internet of thing technology, like the visual programming tool NODERED or the MQTT protocol and server.

That’s what Blynk is for. Started in 2016 as a kickstarter  campaign, they have built a tool which hides a lot of the complexity of the Internet of Things. Blynk consists of the following parts:

Blynk app. With this app you can build a User Interface in just a few minutes. You have all the usual elements like switches, slider,  graphs and much more for controlling IOT devices.

Blynk Server / Cloud is responsible for the communications between the smart phone and IOT devices. There is nothing to do, everything works in the background

IOT devices library: So far everything is very simple. But at the end you have to program your IOT device – the body interaction 2 board for example. They support a great number of boards. For this they created the blynk  library – with the library you need only some lines of code which must be uploaded to the board. Even when you change the user interface the code can stay the same. At least for simple changes. They offer a code generator where you code for your board and use case is generated automatically.

You find a lot of information in the Internet about the pros and cons. In short: It is easy compared to other tools, but if you want to implement your own algorithms programming knowledge is needed. Blynk limits the number of free User Interface elements. If you need more you have to pay  a small fee.

 

Here is short tutorial to run you BI2 with Blynk. (takes 30 minutes)

Download the Blynk app (Android or iPhone).

Within the Blynk app: Register for Blynk and get AUTHenitfication code.

Upload Arduino

You can download Arduino from the Arduino Website or from the Microsoft Store (Windows only)

 

Add or update the following libraries with the library manager: FastLED and Blynk.

Select Include libraries -> library manager

Search for FastLED and install this library (press install button).

Now search for “Blynk” and install the Blynk software. However Blynk suggests to install the Blynk app manually.

 

Add board definition for the ESP8266

Select Tools -> Board -> Board management

Search for ESP and install “esp8266”

Select Board -> Adafruit Feather

Build a connection between BI2 and your computer.

First download the USB driver from here and install the driver. Connect the BI2 with your computer. After some while windows will notice a new device. Windows will communicate with the board over a COM port (e.g. COM3). If you have any problemes check your USB wire. It must support all lines, not only + and – for charging.

Now go back to Arduino and select Tools -> Port. Select the new COM portcom port arduino

Compile and upload the code to BI2 board

Now copy and paste the following code in a Arduino sketch (use File -> new). Then press the Upload button.

/*************************************************************
Controling the body interaction 2 board with the Blynk app
*/

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Auth Token infor the Blynk App.
char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";

// Your WiFi credentials.
char ssid[] = "XXXXXXXX";
char pass[] = "XXXXXXXX";

// Library for controlling the WS2821B (Neopixel) LED or LED strip
#include "FastLED.h"
#define NUM_LEDS 1 // number of LEDs
#define DATA_PIN 14 // pin for LED
CRGB leds[NUM_LEDS]; // define the array of leds

// This function set the LED color according to the selected RGB values in the app.
// RGB values are controlled in the app with zeRGBa widget
// values are stored in the virtual pin V0
// V0 consists of 3 values for Red, Green, Blue
BLYNK_WRITE(V0) // set LED RGB color values
{
  int i = param[0].asInt();
  int j = param[1].asInt();
  int k = param[2].asInt();
  leds[0].setRGB(j,i,k);
  FastLED.show();
}

void setup()
{
  // init LEDs
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

  // connect to Blynk
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
}

You have to change AUTH. Use the AUTH code / token that was sent to you during Blynk registration. Then you have to change the WLAN credentials. Use the name of your network (SSID) and its password. (Depending on the maximum voltage of the vibration motor you have to adjust i, j and k e.g. for a 1.5V motor divide the variables by 3.

Configure the Blynk app

Create a new project and choose this device: ESP8266. Now add user interface elements – they are called widgets -to control the BI2. You can move and resize, add and delete each widget. Press on the widget to enter parameters like GPIO port etc. The app could look like that but you may position the widgets as you like. [The pitch, roll, yaw text fields can be omitted. They are introduced later.]

Add the following Widgets

Press the “+” button and add this widget:

zeRGBA: With this tool you can control the WS2821B LED

Choose select pin V0 = Virtual Pin 0.

Choose Merge.

Send on release: off

Add Sliders

Press the “+” button and add two sliders, one for each motor.

1st silder: Select Digital Pin 12 (PWM)

2nd slider:  Select Digital Pin 13 (PWM)

Add Button

Press the “+” button and add the button widget. Select pin digital – gp0. Set mode to “switch”.

Start the app

Therefore press the Run (or play) button (top left).

 

Done!

Questions? Reply to this post, via wordpress or to jacardano@gmail.co

 

Connecting a servo motor – move your vibrator

The body interaction vibrator development board can be connected with additional sensors and actuators. In this post we show how to connect a servo motor. A servo motor can adjust its shaft to be positioned in varies angles. We use a inexpensive SG92R servo which can be positioned in any position between 0° and 180°.

servo-birdNow we can build eg. a linear actuator which could be useful for sex toys. If you have a 3d printer you can build your linear actuator and fix the servo motor. You can download the design here.

servo-from-top

Connecting servo motor and body interaction vibrator development board

The servo motor has 3 wires: ground (-) (black or brown wire), power (+) (red wire) and control (yellow or orange).

servo connection 2pcb-bottomConnect the (+) wire to the body interaction board. You can use the pad on the bottom side as shown on the image.

servo pcb layoutservo-pcb-from-topThen turn the board around to the top side. Now you can solder the black wire to the “GND” (ground) pad. Then solder the orange or yellow control wire to the leftmost pad “PA1”.

Programming the servo motor

The standard Arduino servo library will not work on the body interaction board. But you can use the TinyServo library. Download the library as *.zip file  here or here and read the forum post.

Go into the Arduino library manager and include the ZIP file. Please restart Arduino.

The following script attaches the servo motor and shows how to control it.

// servo control with the body interaction development board using the TinyServo library
// -- adaption of the demo script by
// tylernt@gmail.com's ATTiny Hardware Timer Assisted Servo Library v1.0 20-Nov-13
// http://forum.arduino.cc/index.php?action=dlattach;topic=198337.0;attach=71790

#include &lt;TinyServo.h&gt;
const byte SERVOS = 1; // number of servos is 1
const byte servoPin[SERVOS] = { 7 }; // servo is connected to PA1 which is pin 7
#define SERVO 0 // our servo is given the name &quot;SERVO&quot;

void setup() {
  setupServos();
}

void loop() {
  delay(1000);
  moveServo(SERVO, 180); // move servo to 180°
  delay(1000);
  moveServo(SERVO, 0); // move servo to 0
  delay(1000);
  for (int i = 0; i &lt;= 180; i++) {
    moveServo(SERVO, i); // move servo from 0° to 180° in 1° steps
    delay(50);
  }
  moveServo(SERVO, 0); // move servo to 0°
  delay(1000);
}

 

 

Programming Tutorial part 4: Sinus

Nervous Optic by Ben Felten, CC BY-ND 2.0

Nervous Optic by Ben Felten, CC BY-ND 2.0

In tutorial 3 – “ramps” we learned how to repeat instructions again and again using the for statement. In this tutorial we need the for loop again, bur instead of changing the motor speed by a constant value we want a more dynamic behavior. Therefore we use the sinus function – a classical pattern used for controlling vibrators.

Here is a straight forward approach:

for (float i = 0; i < 20000; i = i + 0.05) {
  analogWrite(motor, (sin(i)); //motor speed set to sin(i) 
} 
delay(20); 

We are changing the variable i in small steps of 0.05. So the variable i will become 0, 0.05, 0.1, 0.15, 0,2 … and so on.

But this doesn’t work. Let’s have a look at the sinus function. Just use the google search and type in “sin(x)”. You will see  the following curve:

sin(x) in google

There are two problems:

  • There are values below 0 (on the vertical or y-axis). If the value is 0 or below 0 the motor is off.
  • The maximal value is 1. But  we need values between the minimal motor speed (around 40) and the maximal speed (always 255).

You can try to adjust the function and visualize it with google search. Maybe you will discover an interesting variant of the sinus curve.

We use the following function:

(sin(x)+1) 0.5 * (maximal motor speed – minimal motor speed)) + minimal motor speed

  • Sin(x)+1: add 1 to get positive values only between 0 and 2 instead of -1 and 1.
  • Multiply by 0.5: get values between 0 an 1 instead 0 and 2
  • Multiply with maximal motor speed  (255) – minimal motor speed (40): values are now between 0 and 215
  • Add minimal speed: values are between 40 and 255. So the motor will always be on.

 

google_sin

This is the script. Please have a look at tutorial 2 if you don’t know how to upload the script.

 // www.bodyinteraction.com tutorial sinus 
int motor = 3; 
int minimal_motorspeed = 50; 

void setup() { 
  pinMode(motor, OUTPUT); 
} 
void loop() { 
  for (float i = 0; i < 20000; i = i + 0.05) { 
    analogWrite(motor, ((sin(i) + 1) * 0.5 * 215) + 40); 
    delay(5); 
  } 
} 

If you want to slow down the changes in the motor speed change delay(5) and take larger values.

Go back to tutorial 3: ramps

How a program for the body interaction 1 works

The P5_fin script (program) is the basic Arduino script which demonstrates all functions available:

  • The speed of the vibration motor is controlled by movements.
  • Movement data are sent to other body interaction boards.
  • Vibration strength is adjusted when near by body interaction boards have different speed

BI1 interaction pattern

How it works:

The P5_fin script reads data from the accelerometer. Depending of the measured motion the vibration motor is speed up or slowed down. (Slow motion = reduce vibration motor speed, fast motion = speed up).

When the measured motion changes the motion data are sent out and can be received by other BI1. At the same time this script listen to other body interaction boards which send their motion data to all listening nodes.

CodebenderWithBI script and parametersWhen the measured motion of other body interaction board are different from the motion measured by this script, the vibration motor is adjusted (speed up or slowed down). So two or more body interaction boards can influence each other and synchronize after some time. The documented script is available in codebender. In this how-to the uploading of the script is explained.

 

Programming with Codebender

CodebenderWithBI script and parametersCodebender is a great browser-based programming tool. Especially their support for non-standard Arduino platforms like the popular ATtiny microcontrollers is impressive. Before codebender you had to install the actual Arduino software plus the ATtiny core plus compiler updates…

With codebender you select the Arduino core and that’s it. In the new how-to we show you how-to upload a Arduino script to the body interaction 1 (BI1) board. And we present you a script for controlling the  BI1 and how BI1 communicate wireless with one another.

%d bloggers like this: