The IOS-Bluetooth-Controlled-LED project is designed to help you explore and grasp the concept of using an iPhone app to control an Arduino. By creating a simple setup where an iPhone app controls an RGB LED, this project serves as an ideal starting point for understanding Bluetooth communication between iOS devices and Arduino. It provides a solid foundation for developing more complex projects and leveraging the integration of hardware and software to create innovative and interactive applications.
- Main Components
- Understanding the Bluetooth Control Mechanism
- Setup and Usage Guide
- Conclusion
- Resources
- License
- Contact
Arduino MKR WiFi 1010: Microcontroller used to manage the RGB LED and handle Bluetooth communication using ArduinoBLE.
RGB LED: An LED capable of displaying different colors based on the signals received from the Arduino.
iPhone App: Developed using Swift, this app connects to the Arduino via Bluetooth using CoreBluetooth and sends RGB values to control the LED.
Bluetooth Low Energy (BLE) is a wireless communication technology designed for low power consumption and short-range communication. In BLE, devices are categorized as:
- Central Devices: Devices like smartphones or tablets that initiate connections and interact with peripheral devices.
- Peripheral Devices: Devices like sensors or Arduino boards that advertise their presence and wait for central devices to connect to them.
- The Arduino MKR WiFi 1010 is set up as a BLE peripheral device. It advertises a BLE service with characteristics that the iPhone app can interact with.
- The BLE service has three characteristics corresponding to the red, green, and blue values of the RGB LED.
- Each characteristic has a unique UUID and can be read from or written to by the iPhone app. This allows the app to send RGB values to the Arduino, which then adjusts the LED color accordingly.
BLEService arduinoService("af7c1fe6-d669-414e-b066-e9733f0de7a8");
BLEByteCharacteristic redCharacteristic("bf7c1fe6-d669-414e-b066-e9733f0de7a8", BLERead | BLEWrite);
BLEByteCharacteristic greenCharacteristic("cf7c1fe6-d669-414e-b066-e9733f0de7a8", BLERead | BLEWrite);
BLEByteCharacteristic blueCharacteristic("df7c1fe6-d669-414e-b066-e9733f0de7a8", BLERead | BLEWrite);- The iPhone app is developed using the CoreBluetooth framework, which allows it to act as a BLE central device. It scans for nearby BLE peripherals and connects to the Arduino.
- Once connected, the app can read from and write to the RGB characteristics, sending commands to the Arduino to control the LED colors.
let arduinoService = CBUUID(string: "af7c1fe6-d669-414e-b066-e9733f0de7a8")
let redCharacteristic = CBUUID(string: "bf7c1fe6-d669-414e-b066-e9733f0de7a8")
let greenCharacteristic = CBUUID(string: "cf7c1fe6-d669-414e-b066-e9733f0de7a8")
let blueCharacteristic = CBUUID(string: "df7c1fe6-d669-414e-b066-e9733f0de7a8")- The Arduino starts advertising its BLE service as soon as it is powered on. It continuously broadcasts its presence along with the service UUID.
- The iPhone app scans for BLE devices in its vicinity. When it detects the Arduino's advertisement, it identifies the Arduino using the service UUID.
func scanForPeripherals() {
centralManager.scanForPeripherals(withServices: [arduinoService], options: nil)
}- Upon discovering the Arduino, the iPhone app attempts to connect to it.
- Once connected, the app stops scanning and proceeds to discover the available services and characteristics on the Arduino.
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
centralManager.connect(peripheral, options: nil)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.discoverServices([arduinoService])
}- After establishing a connection, the iPhone app discovers the services offered by the Arduino.
- It then discovers the characteristics for the RGB values within the service.
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services ?? [] {
peripheral.discoverCharacteristics([redCharacteristic, greenCharacteristic, blueCharacteristic], for: service)
}
}- The app can read the current values of the RGB characteristics to understand the initial state of the LED.
- To change the LED color, the app writes new values to the characteristics. Each write operation sends a command to the Arduino to update the LED accordingly.
func sendData(_ peripheral: CBPeripheral, r: Data, g: Data, b: Data) {
if let redCBCharacteristic = redCBCharacteristic {
peripheral.writeValue(r, for: redCBCharacteristic, type: .withResponse)
}
if let greenCBCharacteristic = greenCBCharacteristic {
peripheral.writeValue(g, for: greenCBCharacteristic, type: .withResponse)
}
if let blueCBCharacteristic = blueCBCharacteristic {
peripheral.writeValue(b, for: blueCBCharacteristic, type: .withResponse)
}
}By setting up the Arduino as a BLE peripheral and the iPhone as a BLE central, the project allows for wireless control of an RGB LED. The iPhone app communicates with the Arduino by reading and writing to BLE characteristics, enabling dynamic control over the LED colors. This setup provides a foundational framework for developing more advanced projects where an iPhone can control various devices via Bluetooth and an Arduino.
- Arduino MKR WiFi 1010
- RGB LED
- Connecting wires
- Arduino IDE
- Xcode
- iOS device with Bluetooth capabilities
-
Download the Code:
- Download the Arduino code and the Xcode project files from the repository.
-
Open the Arduino Code:
- Open the Arduino IDE and load the provided
.inofile. - Upload the code to your Arduino MKR WiFi 1010.
- Open the Arduino IDE and load the provided
-
Set Up the Hardware:
- Connect the RGB LED to the appropriate pins on the Arduino.
-
Run the iOS App:
- Open the Xcode project.
- Connect your iPhone and run the app.
-
Enjoy and Expand:
- Use the iPhone app to control the RGB LED colors.
- Utilize this project as a foundation to develop your own projects where your iPhone controls an Arduino through Bluetooth, enabling the creation of innovative and interactive applications.
The IOS-Bluetooth-Controlled-LED project is an excellent starting point for understanding Bluetooth communication between an iPhone and Arduino. It provides a foundation for more complex projects and helps in learning the integration of hardware and software to create interactive and controllable systems. By building on this project, you can expand your skills to develop custom applications where your iPhone manages an Arduino through Bluetooth, opening up possibilities for creating unique and interactive technological solutions.
- ArduinoBLE Library: ArduinoBLE
- CoreBluetooth Documentation: CoreBluetooth
- SwiftUI Documentation: SwiftUI
This project is licensed under the MIT License - see the LICENSE file for details.
- Email: adam.ress@icloud.com