This week's assignment is to build and program a smart device using an Arduino UNO that is controlled by Phone or PC
I wanted to use RGB LED and servo motor to simulate a smart gate system. The servo motor is responsible for opening and closing the gate wirelessly, while the RGB LED visually indicates the state of the gate.
For example, when the gate is opening, the red LED will light up, and when the gate is fully open, the Blue LED will light up. If the gate is closed, the LEDs will be off, signaling that the gate is inactive.
TinkerCad
I used Tinkercad to build and test the circuit connections and ensure everything was functional before installation.
Materials used in Tinkercad
1 Breadboard
1 Arduino UNO
1 Servo motor
1 RGB LED
3 Resistors
Materials
1 Breadboard
1 Arduino UNO
1 Servo motor
1 RGB LED
3 Resistors
1 Bluetooth module
I used Tinkercad to build and test the circuit connections, ensuring everything was functional.
I connected the 5V from the Arduino to the positive rail on the breadboard.
Connect the GND from the Arduino to the negative rail on the breadboard.
Connect the servo motor's ground to the Arduino's GND and its power to the positive rail on the breadboard, with the signal pin connected to Digital Pin 13.
Connect the LED's ground to the Arduino's GND, the red pin to 12, the blue pin to 10, and the green pin to 9 on Arduino.
Connections
For the code, I used text-based coding. The code controls two things: an RGB LED and a servo motor. It gets the input through Mobile GUI with a Bluetooth module, and based on what it receives, it performs the following actions:
When the code receives '1':
It turns on the red LED.
It moves the servo motor to an angle of 120 degrees.
When the code receives '2':
It turns on the green LED.
It moves the servo motor to an angle of 140 degrees.
If anything else is received (or no input at all):
It turns off all the LEDs.
It moves the servo motor back to its starting position (90 degrees).
Code
I recreated the Tinkercad circuit using hardware components.
*steps:
Connect the 5V from the Arduino to the positive rail on the breadboard.
Connect the GND from the Arduino to the negative rail on the breadboard.
Connect the servo motor's ground to the Arduino's GND and its power to the positive rail on the breadboard, with the signal pin connected to Digital Pin 13.
Connect the LED's ground to the Arduino's GND, the red pin to 12, the blue pin to 10, and the green pin to 9 on Arduino.
Building the circuit
The RGB LED was likely blinking, after some searching i figured out that the Serial.read() function reads only one character at a time, and each time the loop runs, the input is processed very quickly.
the solution is to store the last valid input in a variable and only change the LED state when new data is received.
For example
void loop() {
if (Serial.available() > 0) {
long incomingData = Serial.read(); // that part where we store data
if (incomingData == '1') {
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
myServo.write(140);
}