1 of 21

Unit 1 - Training

Lab 1D : Bluetooth Arduino with Ultrasound Sensor

UCLA Physics Department

1

University of California, Los Angeles

Department of Physics and Astronomy

Physics 4AL

2 of 21

Outline of Lab 1D

  • 1D In-Lab
    • Connect bluetooth module and ultrasound sensor to Arduino
    • Connect Arduino bluetooth to computer
    • Wirelessly obtain ultrasonic sensor data

UCLA Physics Department

3 of 21

Bluetooth Module

UCLA Physics Department

3

4 of 21

HC-06 Bluetooth module

  • HC-06 is a Bluetooth module designed for establishing short range wireless data communication (<100 meters).
  • This module has the ability to transmit files at speed up to 2.1Mbps and works on Bluetooth 2.0 communication protocol.
    • Operating Voltage: 3.3V - 6V
    • Operating Frequency range: 2.402 GHz - 2.480 GHz

UCLA Physics Department

5 of 21

Bluetooth module names

  • Each table has access to two bluetooth module.
  • The name of the bluetooth modules with the associated table number can be seen to the left. They have been marked on the module as well.

UCLA Physics Department

Table number

Bluetooth module name

2

T2_1,T2_2

3

T3_1,T3_2

4

T4_1,T4_2

5

T5_1,T5_2

6

T6_1,T6_2

7

T7_1,T7_2

8

T8_1,T8_2

9

T9_1,T9_2

10

T10_1,T10_2

6 of 21

Bluetooth circuit

  • Requirements:
    • Arduino Uno
    • Wires
    • HC-06 bluetooth module
  • Retain your circuit built in Lab 1D and add the bluetooth module with the following connections.
  • The green wire is between pin 11 and RXD on bluetooth module.
  • The purple wire is between pin 10 and TXD on bluetooth module

UCLA Physics Department

7 of 21

Battery holder case

  • Follow the instructions in the battery holder case and assemble 4 of your batteries accordingly. Close the lid after you are done.

UCLA Physics Department

8 of 21

TA Checkpoint 1 - Final circuit

  • Your setup after connecting all your components should look similar to this.

UCLA Physics Department

9 of 21

Using the Bluetooth module

UCLA Physics Department

9

10 of 21

Arduino code - Initialization

  • The final code is available for download on slide 13.

  • We will be using the SoftwareSerial library from Arduino by using the command #include<SoftwareSerial.h>.
  • Libraries are files written in C or C++ (.c, .cpp) which provide Arduino codes with extra functionality.

UCLA Physics Department

11 of 21

Arduino code - Initialization

  • The SoftwareSerial library has been developed to allow serial communication on other digital pins of the Arduino.
  • By using SoftwareSerial mySerial(10,11), digital pins 10 and 11 on the Arduino boards are used as virtual RX and TX serial lines.
  • Using mySerial() instead of Serial will communicate with a different COM port which is receive the data transmitted via the bluetooth module.

  • The other 3 lines have been used before in the arduino code for 1E. “Cm” will store the distance of the sensor from the reflecting surface in cm.

UCLA Physics Department

12 of 21

Arduino code - void setup()

  • PIN 10 is set as INPUT and PIN 11 is set as OUTPUT (Receiver and Transmitter respectively).
  • Opening the serial monitor after uploading the code when connected to the Arduino will display the Serial.println() string.
  • Opening the serial monitor when connected to the Bluetooth COM port will display the mySerial.println() string.

UCLA Physics Department

13 of 21

Arduino code - void loop()

  • Instead of Serial.println(), we use mySerial.println() to send our outputs to the Bluetooth COM.

Final Arduino code

  • Upload the code to the Arduino. You have to connect the Arduino using the cable to upload code.

UCLA Physics Department

14 of 21

Connect to batteries

  • Remove the USB cable connecting the Arduino to the PC and connect the setup to the batteries. The Bluetooth module red LED should begin blinking.

UCLA Physics Department

15 of 21

Connect to PC

  • Turn on the Bluetooth on your PC
    • Add a device -> select Bluetooth.
    • Select the device for your table (info on Slide 4).
  • Eg : Here the device is called TEST.

15

16 of 21

Connect to PC

  • When prompted for the password type 1234 and Connect.
  • Your device is now paired with your PC!

16

17 of 21

Connect to PC (Macbook)

17

18 of 21

TA Checkpoint 2 - Getting data from Bluetooth

  • In your Arduino IDE select any COM port and open the Serial Monitor. You should be able to see your ultrasonic sensor values. If this does not work, select a different COM port and repeat.
  • When the Bluetooth is successfully communicating with your PC, the red LED will stay red constantly without blinking. If the red LED continues to blink then they are not yet connected.
  • Your TA will verify if you are receiving the ultrasonic sensor values on Serial Monitor.
  • Please take a screenshot of your readings on the Serial Monitor and a picture of your setup.

UCLA Physics Department

19 of 21

Bluetooth troubleshooting

  • You receive output via the USB in the Serial monitor but not when powered by the battery. This is probably because the battery has lost charge. Use a multimeter to test the voltage in the battery, it is usually the batteries that are closer to 1.2 V that give some trouble with the ultrasound sensor while batteries that are 1.3 V and above work perfectly well.
  • COM Port is busy. The fix has been to close Arduino IDE and turn off Bluetooth on your PCs and turn on Bluetooth again. If this still does not work, then you can unpair the Bluetooth module and then pair it again and this usually works.
  • Noisy or scattered data. This is probably because the battery has lost charge. It is usually the batteries that are closer to 1.2 V that give some trouble with the ultrasound sensor while batteries that are 1.3 V and above work perfectly well.
  • If still not working then unpair all the bluetooth devices that your computer is connected with, then connect your bluetooth module and try again.
  • For additional troubleshooting, refer to this document

UCLA Physics Department

20 of 21

TA Checkpoint 3 - Return bluetooth module

  • Disconnect the battery holder, take out your batteries and remove the Bluetooth module from your setup
  • Place the Bluetooth module back into the container with the other modules
  • If you haven’t already been given one, ask your TA for a clear plastic box. You can use this to store your Arduino setup

20

21 of 21

Post-Lab Requirements for lab 1D

  • Provide the screenshot of your readings on the Serial Monitor and a picture of your setup in your Post-lab Week 2.

Make a copy of this document to submit the post-lab for Week 2.

21