Arduino-esp32: Classic Bluetooth lecture problem

Created on 15 Jan 2019  路  5Comments  路  Source: espressif/arduino-esp32

What you are trying to do?

I'm sending lectures from different sensor via classic serial Bluetooth, I have done it previosly but in this case when I read the lecture of a Heart Sensor once I activate the library is always turning the PIN lecture to 1023, if I read it just with normal serial is working, I have checked different pins and also putting to ground and read 0.

----------------------------- Remove above -----------------------------

Hardware:

Board: ESP32 Dev Module
Core Installation version: 1.6
IDE name: Platform.io
Upload Speed: 115200
Computer OS: Windows 10

This sketch is working correctly using just Serial
#include <Arduino.h>
#include <SparkFunTMP102.h>

// For GSR
// Declaration of functions
void GSRCalculation();
// Input PIN
const int GSRInput = 35;
// Variables
int sensorValue = 0;
int gsr_average = 0;
long sum = 0;
int userResistence = 0;

// For BPM
// Declaration of functions
int myTimer1(long delayTime, long currentMillis);
int myTimer2(long delayTime2, long currentMillis);
void BPMCalculation();

// Input PIN
const int BPMInput = 26;

// Variables
int UpperThreshold = 518;
int LowerThreshold = 490;
int reading = 0;
float BPM = 0.0;
int bpmInt;
bool IgnoreReading = false;
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;

// Measure every 500 seconds
const unsigned long delayTime = 10;
const unsigned long delayTime2 = 3000;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;

// Save previous state
int bpmPrevious = 60;
int bpmIntCurrent;
int bpmIntPrevious;

// For Calories Calculator
int CaloriesBurned(unsigned long _exerciseTime);
int age = 26;
int weight = 72;
int metValue = 5; // https://sites.google.com/site/compendiumofphysicalactivities/Activity-Categories/conditioning-exercise
unsigned long exerciseTime;
int totalCalories;

// For Temperature
void CalculateTemperature();
TMP102 sensor0(0x48); // Initialize sensor at I2C address 0x48
float temperature;

void setup()
{
  Serial.begin(115200);

  pinMode(GSRInput, INPUT);
  pinMode(BPMInput, INPUT);

  // Temperature sensor setup
  sensor0.begin(); // Join I2C bus
  // set the Conversion Rate (how quickly the sensor gets a new reading)
  //0-3: 0:0.25Hz, 1:1Hz, 2:4Hz, 3:8Hz
  sensor0.setConversionRate(3);
  //set Extended Mode.
  //0:12-bit Temperature(-55C to +128C) 1:13-bit Temperature(-55C to +150C)
  sensor0.setExtendedMode(0);
}

void loop()
{
  exerciseTime = millis();

  //Calculate beat pear minute
  BPMCalculation();
  bpmIntCurrent = bpmInt;
  if (bpmIntCurrent != bpmIntPrevious && exerciseTime > 5000)
  {
    Serial.print(bpmInt);
    Serial.println(" BPM");

    GSRCalculation();
    Serial.print("User resistence ");
    Serial.println(userResistence);

    CalculateTemperature();
    Serial.print("Temperature: ");
    Serial.println(temperature);

    // Calculate calories burned
    // totalCalories = CaloriesBurned(exerciseTime);
    // Serial.print(totalCalories);
    // Serial.println(" total calories");
  }

  bpmIntPrevious = bpmIntCurrent;
}

void GSRCalculation()
{
  sum = 0;

  for (int i = 0; i < 10; i++) //Average the 10 measurements to remove the glitch
  {
    sensorValue = analogRead(GSRInput);
    sensorValue = map(sensorValue, 0, 4095, 0, 1023);
    // Serial.println(sensorValue);
    // sensorValue = sensorValue - 140;
    sum += sensorValue;
    delay(5);
  }
  gsr_average = sum / 10;

  // Serial.print("GSR Average ");
  // Serial.println(gsr_average);

  /*
  Human Resistance = ((1024+2*Serial_Port_Reading)*10000)/(512-Serial_Port_Reading), 
  unit is ohm, Serial_Port_Reading is the value display on Serial Port(between 0~1023)
  */

  userResistence = ((1024 + 2 * gsr_average) * 10000) / (512 - gsr_average);
}

void BPMCalculation()
{
  // Get current time
  unsigned long currentMillis = millis();

  // First event
  if (myTimer1(delayTime, currentMillis) == 1)
  {

    // Raw reading of the sensor
    reading = analogRead(BPMInput);

    // The ESP32 has a ADC of 12 bits so map again like if it were of 10 bits
    reading = map(reading, 0, 4095, 0, 1023);

    // Heart beat leading edge detected.
    if (reading > UpperThreshold && IgnoreReading == false)
    {
      if (FirstPulseDetected == false)
      {
        FirstPulseTime = millis();
        FirstPulseDetected = true;
      }
      else
      {
        SecondPulseTime = millis();
        PulseInterval = SecondPulseTime - FirstPulseTime;
        FirstPulseTime = SecondPulseTime;
      }
      IgnoreReading = true;
    }

    // Heart beat trailing edge detected.
    if (reading < LowerThreshold && IgnoreReading == true)
    {
      IgnoreReading = false;
    }

    /*If you know the interval between heart beats, you can calculate the 
    frequency using the formula: frequency = 1 / time. Now if you take 
    the frequency and multiply it by 60, you should get BPM.*/

    // Calculate Beats Per Minute.
    BPM = (1.0 / PulseInterval) * 60.0 * 1000;
  }

  // Second event
  if (myTimer2(delayTime2, currentMillis) == 1)
  {
    // Serial.print(reading);
    // Serial.print("\t");
    // Serial.print(PulseInterval);
    // Serial.print("\t");

    if (BPM > 220 || BPM < 59)
    {
      BPM = bpmPrevious;
    }

    // Cast to int
    bpmInt = (int)BPM;

    bpmPrevious = BPM;
  }
}

// First event timer
int myTimer1(long delayTime, long currentMillis)
{
  if (currentMillis - previousMillis >= delayTime)
  {
    previousMillis = currentMillis;
    return 1;
  }
  else
  {
    return 0;
  }
}

// Second event timer
int myTimer2(long delayTime2, long currentMillis)
{
  if (currentMillis - previousMillis2 >= delayTime2)
  {
    previousMillis2 = currentMillis;
    return 1;
  }
  else
  {
    return 0;
  }
}

int CaloriesBurned(unsigned long _exerciseTime)
{
  // https://www.cmsfitnesscourses.co.uk/blog/53/using-mets-to-calculate-calories
  unsigned long exerciseTimeMinutes = _exerciseTime / 60000;
  int calories = metValue * weight / 200 * exerciseTimeMinutes;
  return calories;
}

void CalculateTemperature()
{
  // Turn sensor on to start temperature measurement.
  sensor0.wakeup();

  // read temperature data
  temperature = sensor0.readTempC();

  // Place sensor in sleep mode to save power.
  // Current consumtion typically <0.5uA.
  sensor0.sleep();
}
This sketch is not working once I use class serial bluetooth
#include <Arduino.h>
#include <SparkFunTMP102.h>
#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

// For GSR
// Declaration of functions
void GSRCalculation();
// Input PIN
const int GSRInput = 35;
// Variables
int sensorValue = 0;
int gsr_average = 0;
long sum = 0;
int userResistence = 0;

// For BPM
// Declaration of functions
int myTimer1(long delayTime, long currentMillis);
int myTimer2(long delayTime2, long currentMillis);
void BPMCalculation();

// Input PIN
const int BPMInput = 26;

// Variables
int UpperThreshold = 518;
int LowerThreshold = 490;
int reading = 0;
float BPM = 0.0;
int bpmInt;
bool IgnoreReading = false;
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;

// Measure every 500 seconds
const unsigned long delayTime = 10;
const unsigned long delayTime2 = 3000;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;

// Save previous state
int bpmPrevious = 60;
int bpmIntCurrent;
int bpmIntPrevious;

// For Calories Calculator
int CaloriesBurned(unsigned long _exerciseTime);
int weight = 72;
int metValue = 5; // https://sites.google.com/site/compendiumofphysicalactivities/Activity-Categories/conditioning-exercise
unsigned long exerciseTime;
int totalCalories;

// For Temperature
void CalculateTemperature();
TMP102 sensor0(0x48); // Initialize sensor at I2C address 0x48
float temperature;

void setup()
{
  Serial.begin(115200);

  pinMode(GSRInput, INPUT);
  pinMode(BPMInput, INPUT);

  // Temperature sensor setup
  sensor0.begin(); // Join I2C bus
  // set the Conversion Rate (how quickly the sensor gets a new reading)
  //0-3: 0:0.25Hz, 1:1Hz, 2:4Hz, 3:8Hz
  sensor0.setConversionRate(3);
  //set Extended Mode.
  //0:12-bit Temperature(-55C to +128C) 1:13-bit Temperature(-55C to +150C)
  sensor0.setExtendedMode(0);

  SerialBT.begin("ESP32Emotion"); //Bluetooth device name
}

void loop()
{
  exerciseTime = millis();

  //Calculate beat pear minute
  BPMCalculation();
  bpmIntCurrent = bpmInt;
  if (bpmIntCurrent != bpmIntPrevious && exerciseTime > 5000)
  {
    // Serial.print(bpmInt);
    // Serial.println(" BPM");

    // GSRCalculation();
    // Serial.print("User resistence ");
    // Serial.println(userResistence);

    // CalculateTemperature();
    // Serial.print("Temperature: ");
    // Serial.println(temperature);

    // Calculate calories burned
    // totalCalories = CaloriesBurned(exerciseTime);
    // Serial.print(totalCalories);
    // Serial.println(" total calories");
  }

  bpmIntPrevious = bpmIntCurrent;
}

void GSRCalculation()
{
  sum = 0;

  for (int i = 0; i < 10; i++) //Average the 10 measurements to remove the glitch
  {
    sensorValue = analogRead(GSRInput);
    sensorValue = map(sensorValue, 0, 4095, 0, 1023);
    // Serial.println(sensorValue);
    // sensorValue = sensorValue - 140;
    sum += sensorValue;
    delay(5);
  }
  gsr_average = sum / 10;

  // Serial.print("GSR Average ");
  // Serial.println(gsr_average);

  /*
  Human Resistance = ((1024+2*Serial_Port_Reading)*10000)/(512-Serial_Port_Reading), 
  unit is ohm, Serial_Port_Reading is the value display on Serial Port(between 0~1023)
  */

  userResistence = ((1024 + 2 * gsr_average) * 10000) / (512 - gsr_average);
}

void BPMCalculation()
{
  // Get current time
  unsigned long currentMillis = millis();

  // First event
  if (myTimer1(delayTime, currentMillis) == 1)
  {

    // Raw reading of the sensor
    reading = analogRead(BPMInput);

    // The ESP32 has a ADC of 12 bits so map again like if it were of 10 bits
    reading = map(reading, 0, 4095, 0, 1023);

    SerialBT.println(reading);

    // Heart beat leading edge detected.
    if (reading > UpperThreshold && IgnoreReading == false)
    {
      if (FirstPulseDetected == false)
      {
        FirstPulseTime = millis();
        FirstPulseDetected = true;
      }
      else
      {
        SecondPulseTime = millis();
        PulseInterval = SecondPulseTime - FirstPulseTime;
        FirstPulseTime = SecondPulseTime;
      }
      IgnoreReading = true;
    }

    // Heart beat trailing edge detected.
    if (reading < LowerThreshold && IgnoreReading == true)
    {
      IgnoreReading = false;
    }

    /*If you know the interval between heart beats, you can calculate the 
    frequency using the formula: frequency = 1 / time. Now if you take 
    the frequency and multiply it by 60, you should get BPM.*/

    // Calculate Beats Per Minute.
    BPM = (1.0 / PulseInterval) * 60.0 * 1000;
  }

  // Second event
  if (myTimer2(delayTime2, currentMillis) == 1)
  {
    // Serial.print(reading);
    // Serial.print("\t");
    // Serial.print(PulseInterval);
    // Serial.print("\t");

    if (BPM > 220 || BPM < 59)
    {
      BPM = bpmPrevious;
    }

    // Cast to int
    bpmInt = (int)BPM;

    bpmPrevious = BPM;
  }
}

// First event timer
int myTimer1(long delayTime, long currentMillis)
{
  if (currentMillis - previousMillis >= delayTime)
  {
    previousMillis = currentMillis;
    return 1;
  }
  else
  {
    return 0;
  }
}

// Second event timer
int myTimer2(long delayTime2, long currentMillis)
{
  if (currentMillis - previousMillis2 >= delayTime2)
  {
    previousMillis2 = currentMillis;
    return 1;
  }
  else
  {
    return 0;
  }
}

int CaloriesBurned(unsigned long _exerciseTime)
{
  // https://www.cmsfitnesscourses.co.uk/blog/53/using-mets-to-calculate-calories
  unsigned long exerciseTimeMinutes = _exerciseTime / 60000;
  int calories = metValue * weight / 200 * exerciseTimeMinutes;
  return calories;
}

void CalculateTemperature()
{
  // Turn sensor on to start temperature measurement.
  sensor0.wakeup();

  // read temperature data
  temperature = sensor0.readTempC();

  // Place sensor in sleep mode to save power.
  // Current consumtion typically <0.5uA.
  sensor0.sleep();
}

This is the output of the pin that I'm reading

capture

Most helpful comment

adc does not work on those pins when Radio is on (WiFi or BT). try to read a pin above 33 :)

All 5 comments

adc does not work on those pins when Radio is on (WiFi or BT). try to read a pin above 33 :)

Thanks I will prove it!, one more question, if possible could you tell me which PIN can be used?

Here is a list of reserved GPIO's and here a list for ADC specific

Confirming, just pins:

IO34 | GPIO34,聽ADC1_CH6, RTC_GPIO4
IO35 | GPIO35,聽ADC1_CH7, RTC_GPIO5
IO32 | GPIO32, XTAL_32K_P (32.768 kHz crystal oscillator input),聽ADC1_CH4, TOUCH9, RTC_GPIO9
IO33 | GPIO33, XTAL_32K_N (32.768 kHz crystal oscillator output),聽ADC1_CH5, TOUCH8, RTC_GPIO8

Can be used when the radio is enabled?

ADC2 functions are unavailable while WiFi,BlueTooth are in use. The Pins can be uses for other functions like GPIO.

If you want to use Analog to Digital Conversion while Wifi or BlueTooth is in use, you have to use ADC1 pins :

  • GPIO32 input/output, ADC4
  • GPIO33 input/output, ADC5
  • GPI 34 input only, ADC6
  • GPI 35 input only, ADC7
  • GPI 36 input only, ADC0
  • GPI 39 input only, ADC3

Chuck.

Was this page helpful?
0 / 5 - 0 ratings