Build a simple smart circuit that contains at least TWO action components that you can control wirelessly.
Use either the Android GUI or PC GUI to control the action components
Inspiration
The servo rotate to acertain angle to show a picture which a part of story.
Also a RGB LED is lighting with adifferent colors.
Software
Breadboard
Servo Motor (OUTPUT)
Recive the order from the bluetooth application and go to the anagle.
Arduino Uno
to control the circuit
RGB LED Light RED, GREEN, Blue (OUTPUT)
to smiulate the Police car lamp.
Resistor
Bluetooth module
9V adapter
the power supply
crocodile wires
to connect the pushbutton to the jumper wires.
male-male jumper wires
to connect the components to the breadboard.
Step 1
Open Fritzing
Step 2
Create a New sketch
Step 3
Start searching for components
First, we will use breadboard
so drag it in workspace.
Step 4
Then add the Arduino.
Add the Arduino.
Add the Servo.
Add the Voltage regolater.
Step 5
-Connect the first pin of the reglutor to + pins of the breadboard and the second pin of the regulator to - of the breadboard.
-connect the Vin pin on Arduino to+ of the breadboard.
-Connect the GND pin on Arduino to - of the breadboard.
-Add external Power source.
-Connect the + of the power source to the third pin of the regulator .
-Connect the - of the power source to the second pin of the regulator.
Step 6
-Connect the + of the Servo to the + pins of the Breadboard .
-Connect the - of the Servo to the - pins of the Breadboard .
-Connect the Signal pin of the Servo to pin 9 of the Arduino .
-Add Bluetooth Module.
-Connect the VCC PIN of the Bluetooth Module to the + pins of the Breadboard .
-Connect the GND PIN of the Bluetooth Module to the - pins of the Breadboard .
-Connect the TX pin of the Servo to pin 0 of the Arduino .
-Connect the RX pin of the Servo to pin 1 of the Arduino
Step 7
-Connect the first pin of the reglutor to + pins of the breadboard and the second pin of the regulator to - of the breadboard.
Step 8
-Add Bluetooth Module.
-Connect the VCC PIN of the Bluetooth Module to the + pins of the Breadboard .
-Connect the GND PIN of the Bluetooth Module to the - pins of the Breadboard .
-Connect the TX pin of the Servo to pin 0 of the Arduino .
-Connect the RX pin of the Servo to pin 1 of the Arduino
Step 9
-Add external Power source.
-Connect the + of the power source to the third pin of the regulator .
-Connect the - of the power source to the second pin of the regulator.
Step 10
-connect the Vin pin on Arduino to+ of the breadboard.
-Connect the GND pin on Arduino to - of the breadboard.
Step 11
-Connect the + of the Servo to the + pins of the Breadboard .
-Connect the - of the Servo to the - pins of the Breadboard .
-Connect the Signal pin of the Servo to pin 9 of the Arduino .
Step 12
Write the servo code
https://arduinogetstarted.com/tutorials/arduino-controls-servo-motor-via-bluetooth
I used the Previous code
Step 13
Add the led
-add the 3resistors
-then connect common cathode terminal of the led to - of the breaddboard.
-connect the RED Terminal of LED to the Pin 3 of arduino ,
-GREEN terminal to Pin 5,
-BLUE terminal to Pin 6.
Step 14
Add the led
2-add the 3resistors
3-connect arduino ground and 5v to breadboard
4-then connect yhe cathode terminal of the led to arduino ground.
5-connect the RED Terminal of LED to the Pin 3 of arduino ,
6-GREEN terminal to Pin 5,
6-BLUE terminal to Pin 6.
Finall led circuit
Step 15
Start the first code
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial Bluetooth(0, 1); // (RX, TX) of HC-05
Servo servoMotor; // Create a servo object
Step 16
void setup() {
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Step 17
void loop() {
if (Bluetooth.available()) {
char receivedChar = Bluetooth.read();
if (receivedChar == '1') {
// Change colors of the RGB LED
digitalWrite(3, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(100); // Wait for 1000 millisecond(s)
} else {
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(100); // Wait for 1000 millisecond(s)
}
Step 16
Combine the 2 Codes
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial Bluetooth(0, 1); // (RX, TX) of HC-05
Servo servoMotor; // Create a servo object
Libraries:
SoftwareSerial.h: This library allows you to create a software serial port on Arduino digital pins.
Servo.h: This library enables control of servo motors.
Global Variables:
Bluetooth: This line creates a SoftwareSerial object named Bluetooth. It specifies the Arduino pins used for serial communication with the HC-05 Bluetooth module (RX on pin 0, TX on pin 1).
servoMotor: This line creates a Servo object named servoMotor, which will be used to control a servo motor.
void setup() {
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
// Initialize serial communication with Bluetooth module
Bluetooth.begin(9600);
// Initialize servo motor
servoMotor.attach(9); // Attach the servo to pin 9
Serial.begin(9600); // Initialize serial communication at 9600 baud rate
}
Setup Function:
Pins 3, 5, and 6 are set as outputs. These pins will control the RGB LED.
Serial communication is initialized with the Bluetooth module at a baud rate of 9600.
The servo motor is attached to pin 9.
Serial communication is initialized at a baud rate of 9600 for communication with the serial monitor.
void loop() {
if (Bluetooth.available()) {
char receivedChar = Bluetooth.read();
if (receivedChar == '1') {
// Change colors of the RGB LED
digitalWrite(3, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(100); // Wait for 1000 millisecond(s)
} else {
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(100); // Wait for 1000 millisecond(s)
}
Loop Function:
The loop continuously checks for data available from the Bluetooth module.
If data is available, it reads the received character.
If the received character is '1', it changes the colors of the RGB LED by turning on and off the corresponding pins.
If no data is available, it turns off all RGB LED pins.
Additionally, the loop checks if there is data available from the serial monitor. If data is available, it reads the angle value for the servo motor.
If the angle is within the valid range (0 to 180), it sets the servo motor angle to the received angle value.
// Control servo motor angle
if (Serial.available() > 0) { // Check if data is available to read
int angle = Serial.parseInt(); // Read the angle value sent via Bluetooth
if (angle >= 0 && angle <= 180) { // Make sure the angle is within valid range
servoMotor.write(angle); // Set the servo angle
Serial.print("Angle set to: ");
Serial.println(angle); // Print the set angle to serial monitor
}
else {
Serial.println("Invalid angle. Please send a value between 0 and 180.");
}
}
}
}
Additionally, the loop checks if there is data available from the serial monitor. If data is available, it reads the angle value for the servo motor.
If the angle is within the valid range (0 to 180), it sets the servo motor angle to the received angle value.
First i used the code below
but the led lighting only one color
#include <SoftwareSerial.h>
#define RED_PIN 3
#define GREEN_PIN 5
#define BLUE_PIN 6
SoftwareSerial Bluetooth(0, 1); // (RX, TX) of HC-05
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
// Initialize serial communication with Bluetooth module
Bluetooth.begin(9600);
}
void loop() {
if (Bluetooth.available()) {
char receivedChar = Bluetooth.read();
if (receivedChar == '45') {
// Change colors of the RGB LED
setColor(255, 0, 0); // Red
delay(10);
setColor(0, 255, 0); // Green
delay(10);
setColor(0, 0, 255); // Blue
delay(10);
setColor(255, 255, 255); // White
delay(10);
setColor(0, 0, 0); // Off
}
}
}
void setColor(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}
Solution I use some parts from the RGB LED code from week7 and make amix from the two code.