Help Regarding Baud Rate

Not super experienced with Arduino; attempting a project which combines a four button R-2R analog input, PIR Sensor and UART DFRobot Voice Recognition module. The latter two use 115200 baud, while the analog input uses 9600 baud- as I understand it, this is the speed at which they send data to the IDE.

When combined together, the button input does not send any input even when the Serial Monitor is set to 9600; all components function correctly when ran independently and the other two components function correctly while combined together. Is it not possible to run devices of different baud rate on the same Arduino? Is there a way to decrease the baud rate of the other two components in the software? Thank you in advance for any help.

"baud" is the serial port bit rate. The analog input (ADC) has nothing to do with that, but the default ADC sample rate happens to be around 9600 samples per second on Arduino Uno and the like. You can print the ADC samples to the serial monitor at 115200 baud or higher.

Please post the code, using code tags, and more details about your project.

//Button Input
int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for asigning the buttons and their high and low values
int Button[16][3] = {{1, 310, 343}, // button 1
                     {2, 179, 218}, // button 2
                     {3, 85, 134}, // button 3
                     {4, 30, 84}, // button 4
                     {5, 418, 438}, // button 1 + button 2
                     {6, 371, 394}, // button 1 + button 3
                     {7, 344, 370}, // button 1 + button 4
                     {8, 256, 290}, // button 2 + button 3
                     {9, 219, 255}, // button 2 + button 4
                     {10, 135, 178}, // button 3 + button 4
                     {11, 931, 932}, //button 1 + button 2 + button 3
                     {12, 395, 417}, //button 1 + button 3 + button 4
                     {13, 291, 309}, //button 2 + button 3 + button 4
                     {14, 439, 457}, //button 1 + button 2 + button 4
                     {15, 478, 1000}, //all inputs
                     {16, 0, 29}, //no input
};
int analogpin = 0; // analog pin to read the buttons
int label = 0;  // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0;  // the last time the output pin was sampled
int debounce_count = 100; // number of millis/samples to consider before declaring a debounced input
int current_state = 0;  // the debounced input value
int current_label = 0; //the debounced input label
int ButtonVal;

//Distance Sensor
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

//Voice Recognition
/*!
 * @file uart.ino
 * @brief  Control the voice recognition module via UART
 * @n  Get the recognized command ID and play the corresponding reply audio according to the ID;
 * @n  Set the wake-up state duration, set mute mode, set volume, enter the wake-up state, and reset the module
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence  The MIT License (MIT)
 * @author  [qsjhyy](yihuan.huang@dfrobot.com)
 * @version  V1.0
 * @date  2022-12-30
 * @url  https://github.com/DFRobot/DFRobot_DF2301Q
 */
#include "DFRobot_DF2301Q.h"

/**
 * @brief DFRobot_URM13_RTU constructor
 * @param serial - serial ports for communication, supporting hard and soft serial ports
 * @param rx - UART The pin for receiving data
 * @param tx - UART The pin for transmitting data
 */
#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266))   // Use software serial
  SoftwareSerial softSerial(/*rx =*/4, /*tx =*/5);
  DFRobot_DF2301Q_UART DF2301Q(/*softSerial =*/&softSerial);
#elif defined(ESP32)   // Use the hardware serial with remappable pin: Serial1
  DFRobot_DF2301Q_UART DF2301Q(/*hardSerial =*/&Serial1, /*rx =*/D3, /*tx =*/D2);
#else   // Use hardware serial: Serial1
  DFRobot_DF2301Q_UART DF2301Q(/*hardSerial =*/&Serial1);
#endif

void setup()
{
  //Distance Sensor
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input

  Serial.begin(115200);

  // Init the sensor
  while( !( DF2301Q.begin() ) ) {
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }
  Serial.println("Begin ok!");

  /**
   * @brief Reset the module
   */
  // DF2301Q.resetModule();

  /**
   * @brief Set commands of the module
   * @param setType - Set type
   * @n       DF2301Q_UART_MSG_CMD_SET_VOLUME : Set volume, the set value range 1-7 
   * @n       DF2301Q_UART_MSG_CMD_SET_ENTERWAKEUP : Enter wake-up state; set value 0
   * @n       DF2301Q_UART_MSG_CMD_SET_MUTE : Mute mode; set value 1: mute, 0: unmute
   * @n       DF2301Q_UART_MSG_CMD_SET_WAKE_TIME : Wake-up duration; the set value range 0-255s
   * @param setValue - Set value, refer to the set type above for the range
   */
  //  DF2301Q.settingCMD(DF2301Q_UART_MSG_CMD_SET_MUTE, 0);
  //  DF2301Q.settingCMD(DF2301Q_UART_MSG_CMD_SET_VOLUME, 5);
  //  DF2301Q.settingCMD(DF2301Q_UART_MSG_CMD_SET_WAKE_TIME, 20);
  // DF2301Q.settingCMD(DF2301Q_UART_MSG_CMD_SET_ENTERWAKEUP, 0);

  /**
   * @brief Play the corresponding reply audio according to the command word ID
   * @param CMDID - Command word ID
   */
  DF2301Q.playByCMDID(23);

}

void loop()
{
  //Button Input
   // If we have gone on to the next millisecond
  if (millis() != time)
  {
    // check analog pin for the button value and save it to ButtonVal
    ButtonVal = analogRead(analogpin);
    ButtonCheck();
    if(label == current_label && counter >0)
    { 
      counter--;
    }
    if(label != current_label)
    {
      counter++;
    }
    // If ButtonVal has shown the same value for long enough let's switch it
    if (counter >= debounce_count)
    {
      counter = 0;
      current_label = label;
      //Checks which button or button combo has been pressed
      if (ButtonVal > 0)
      {
        Action();
      }
    }
    time = millis();
  }

  //Distance Sensor
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH){
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }

  //Voice Recognition
  /**
   * @brief Get the ID corresponding to the command word 
   * @return Return the obtained command word ID, returning 0 means no valid ID is obtained
   */
  uint8_t CMDID = 0;
  CMDID = DF2301Q.getCMDID();
  if(0 != CMDID) {
    Serial.print("CMDID = ");
    Serial.println(CMDID);
  }

  if (CMDID == 5){
    Serial.println("Happy");
  }  
  if (CMDID == 6){
    Serial.println("Sad");
  }
  if (CMDID == 7){
    Serial.println("Angry");
  }
  if (CMDID == 8){
    Serial.println("Surprised");
  }
  if (CMDID == 9){
    Serial.println("Ambivalent");
  }
  if (CMDID == 10){
    Serial.println("Proud");
  }
  if (CMDID == 11){
    Serial.println("Delightful");
  }
  if (CMDID == 12){
    Serial.println("Irritated");
  }
  if (CMDID == 13){
    Serial.println("Disappointed");
  }
  if (CMDID == 14){
    Serial.println("Outraged");
  }
  if (CMDID == 15){
    Serial.println("Morbid");
  }
  if (CMDID == 16){
    Serial.println("Righteous");
  }
  if (CMDID == 17){
    Serial.println("Grief");
  }
  if (CMDID == 18){
    Serial.println("Shocked");
  }
  if (CMDID == 19){
    Serial.println("Overwhelmed");
  }
  delay(2000);

}

void ButtonCheck()
{
  // loop for scanning the button array.
  for(int i = 0; i <= 16; i++)
  {
    // checks the ButtonVal against the high and low vales in the array
    if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
    {
      // stores the button number to a variable
      label = Button[i][0];
      //current_label = label;   
    }
  }
}

void Action()
{
  if(label == 1)
  {
    Serial.println("Happy");
  }
  if(label == 2)
  {
    Serial.println("Sad");
  }
  if(label == 3)
  { 
    Serial.println("Angry");
  }
  if(label == 4)
  {
    Serial.println("Suprised");
  }
  if(label == 5)
  {
    Serial.println("Ambivalent");
  }
  if(label == 6)
  {
    Serial.println("Prideful");
  }
    if(label == 7)
  {
    Serial.println("Delightful");
  }
  if(label == 8)
  {
    Serial.println("Irritated");
  }
  if(label == 9)
  {
    Serial.println("Disappointed");
  }
   if(label == 10)
  {
    Serial.println("Outraged");
  }
  if(label == 11)
  {
    Serial.println("Morbid");
  }
  if(label == 12)
  {
    Serial.println("Righteous");
  }
  if(label == 13)
  {
    Serial.println("Grief");
  }    
   if(label == 14)
  {
    Serial.println("Shock");
  }     
   if(label == 15)
  {
    Serial.println("Overwhelmed");
  }
   if(label == 16)
  {
    Serial.println("No Input");
  }
   
 
}

This is my code for the project; the goal is to create some automated systems for a therapeutic children's toy called a "Worry Monster".

Analog inputs don't use any Baud rate. I never heard of a PIR sensor that used any baud rate either, but please post a link. That voice module uses 9600 baud.

So the things you think you have understood, you have not understood.

I suspect you are looking at example sketches for each of the 3 devices and they don't all use the same baud rate, and you are confused about this. Don't be. The baud rates are only used for communicating between the Arduino and the serial monitor on your pc/laptop, and you are free to use any rate you like for that in your sketch. The baud rates you have seen are not used to communicate between the Arduino and the devices.

Of the 3, baud rate is only relevant for the speech module, and you don't have to use the same baud rate between the Arduino and the speech module and between the Arduino and the serial monitor.

I'm using the infrared sensor from RadioShack's "Make:it Robotics Sensor Kit", in the link below.

The voice module I am using is shown using 115200 baud in the example code provided in the library, and gives a readable output while the Monitor is set to this baud. Why do you say it uses 9600 baud? Why do these components not give a readable output at 9600 baud?

The baud rate in the example code has nothing to do with the voice module. It is merely the communication rate set for the Arduino and the serial monitor.
I refer to this example, taken from the DFRobot product page:

/*!
 * @file  i2c.ino
 * @brief Control the voice recognition module via I2C
 * @n  Get the recognized command ID and play the corresponding reply audio according to the ID;
 * @n  Get and set the wake-up state duration
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence  The MIT License (MIT)
 * @author  [qsjhyy](yihuan.huang@dfrobot.com)
 * @version  V1.0
 * @date  2022-04-02
 * @url  https://github.com/DFRobot/DFRobot_DF2301Q
 */
#include "DFRobot_DF2301Q.h"

#define Led 8

//I2C communication
DFRobot_DF2301Q_I2C asr;

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

  pinMode(Led, OUTPUT);    //Init LED pin to output mode
  digitalWrite(Led, LOW);  //Set LED pin to low 

  // Init the sensor
  while (!(asr.begin())) {
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }
  Serial.println("Begin ok!");

  /**
   * @brief Set voice volume
   * @param voc - Volume value(1~7)
   */
  asr.setVolume(4);

  /**
     @brief Set mute mode
     @param mode - Mute mode; set value 1: mute, 0: unmute
  */
  asr.setMuteMode(0);

  /**
     @brief Set wake-up duration
     @param wakeTime - Wake-up duration (0-255)
  */
  asr.setWakeTime(20);

  /**
     @brief Get wake-up duration
     @return The currently-set wake-up period
  */
  uint8_t wakeTime = 0;
  wakeTime = asr.getWakeTime();
  Serial.print("wakeTime = ");
  Serial.println(wakeTime);

  // asr.playByCMDID(1);   // Wake-up command

  /**
     @brief Play the corresponding reply audio according to the ID
     @param CMDID - command word ID
  */
  //asr.playByCMDID(23);  // Command word ID
}

void loop() {
  /**
     @brief Get the ID corresponding to the command word 
     @return Return the obtained command word ID, returning 0 means no valid ID is obtained
  */
  uint8_t CMDID = asr.getCMDID();
  switch (CMDID) {
    case 103:                                                  //If the command is “Turn on the light”
      digitalWrite(Led, HIGH);                                 //Turn on the LED
      Serial.println("received'Turn on the light',command flag'103'");  //Serial transmits "received"Turn on the light",command flag"103
      break;

    case 104:                                                  //If the command is “Turn off the light”
      digitalWrite(Led, LOW);                                  //Turn off the LED
      Serial.println("received'Turn off the light',command flag'104'");  //The serial transmits "received"Turn off the light",command flag"104""
      break;

    default:
      if (CMDID != 0) {
        Serial.print("CMDID = ");  //Printing command ID
        Serial.println(CMDID);
      }
  }
  delay(300);
}

The voice module communicates with the Arduino via a separate I2C channel, handled by the voice module library.

The best approach to building up a complex project is to start with working code, and add one new thing (sensor, option, etc.) at a time, and get that working before moving on.

The DF2301Q module is also capable of using I2C, so you wouldn't have to mess with SoftwareSerial.

The analog input does not have a baud rate. You're looking at the baud rate for the Serial object. That's got nothing to do with the PIR sensor. Change any PIR example sketch you're looking at to use 115200 with Serial and you'll see it works just the same as at 9600.

If you are using a voice module example that uses SoftwareSerial, don't. Use the I2C example on the product page instead (posted in #6 above).

1 Like

Yeah, it has a simple digital output, not UART.

Because I took a sneaky peek into the library.

Like I said before, 115200 is the baud rate between the Arduino and the serial monitor, not the baud rate between the Arduino and the voice module. You are not seeing the output of the voice module on the serial monitor, you are seeing the output from the Arduino.

Maybe you forgot to set the serial monitor to the same baud that the sketch uses?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.