Consider this Arduino code snippet that sends data over Bluetooth using SoftwareSerial. What data will be sent to the Bluetooth device?
#include <SoftwareSerial.h> SoftwareSerial BTSerial(10, 11); // RX, TX void setup() { BTSerial.begin(9600); Serial.begin(9600); } void loop() { int sensorValue = 123; BTSerial.print("Value:"); BTSerial.println(sensorValue); delay(1000); while(true) {} }
Remember that println adds a newline character after the data.
The print sends "Value:" and println sends the number followed by carriage return and newline characters (\r\n). So the full output is "Value:123\r\n".
When using the SoftwareSerial library to communicate with a Bluetooth module on an Arduino Uno, which pins are typically chosen for RX and TX?
Remember that pins 0 and 1 are used for USB serial communication.
Pins 0 and 1 are reserved for USB serial communication with the PC, so using them for Bluetooth causes conflicts. Pins 10 and 11 are commonly used for SoftwareSerial RX and TX to avoid this conflict.
Look at this Arduino code snippet intended to send "Hello" over Bluetooth. Why does it fail to send the data?
#include <SoftwareSerial.h> SoftwareSerial BTSerial(10, 11); // RX, TX void setup() { Serial.begin(9600); BTSerial.begin(9600); } void loop() { BTSerial.write("Hello"); delay(1000); }
Check the data type expected by write() when sending strings.
The write() function sends raw bytes and expects either a single byte or a byte array with length. Passing a string literal without specifying length causes it to send only the first byte or behave unexpectedly. Using print() or write((const uint8_t*)"Hello", 5) is correct.
Which of the following Arduino code snippets will cause a syntax error when trying to send data over Bluetooth?
Check how string concatenation works in Arduino C++.
Option C tries to add a string literal and an integer directly, which is invalid in Arduino C++. This causes a syntax error. Options A, B, and D use valid methods to send data.
Given this Arduino code sending data over Bluetooth, how many bytes are transmitted each time loop() runs?
#include <SoftwareSerial.h> SoftwareSerial BTSerial(10, 11); // RX, TX void setup() { BTSerial.begin(9600); } void loop() { BTSerial.print("A"); BTSerial.println(123); delay(1000); while(true) {} }
Count each character sent including newline characters added by println.
The print("A") sends 1 byte ('A'). The println(123) sends '1', '2', '3', \r, \n (5 bytes). Total: 6 bytes.