Introduction
In my previous article, I have explained about connecting Python and Arduino. This article is the next level for connecting Arduino and Python with Bluetooth and the Motor. It can be based on the set of the delay time and is based on ON/OFF.
Parts Of List
Hardware side
- Arduino Uno
- Bluetooth HC-05
- Servo Motor
- Bread board
- Hook Up wires
Software Side
- Arduino IDE
- Python software
Connection
Part 1: Connect From Bluetooth To Board
BT |
Board |
TX |
RX |
RX |
TX |
Gnd |
Gnd |
Vcc |
5v |
Part 2: Connect From Servo Motor To Board
Motor |
Board |
Vin |
09 |
Gnd |
Gnd |
Vcc |
5v |
Programming
Program For Arduino Part
- #include " Softwareserial.h"
- softwareSerial serial.connection(10, 11);#
- define BUFFER_SIZE 64
- char inData[BUFFER_SIZE];
- char inchar = -1;
- int count = 0;
- int i = 0;
- void setup()
- {
- serial.begin(9600);
- serial_connection.begin(9600);
- serial_connection.println("Ready!!!");
- serial_println("Started");
- }
- Void loop()
- {
- byte byte = Count = serial_connection.available();
- if (byte_ Count) {
- Serial println("Incoming Data");
- Int first_bytes = bytes - Count;
- int remaining_bytes = 0;
- if (first_bytes > +BUFFER_SIZE - 1)
- {
- remaining_bytes = bytes_count = (BUFFER_SIZE - 1);
- }
- for (i = 0; i < first_byte; i++)
- {
- intchar = Serial_connection.read();
- int Data[i] = inchar;
- }
- intData[i] = '\0';
- if (string(inData) == "BOOP 2")
- {
- Serial.println("************* start Motor ********************");
- }
- elseif(string(inData) == "BOOP 4")
- {
- Serial.println("************* stop Motor ********************");
- }
- for (i = 0; i < remaining_bytes; i++)
- {
- inchar = Serial_connection.read();
- }
- Serial_ConnectionPrintln("Hello From Blue" + string(count));
- Count++;
- }
- delay(100)
- }
Programming For Python Part
- import serial
- import time
- Print ("Start")
- port="/dev/tty.HC-06-DevB"
- bluetooth=serial.serial(port,9600)
- print("Connected"
- bluetooth.flushInput()
- for i in range(5)
- print("ping")
- bluetooth.Write(b"Boop"+str.encode(str(i)))
- input-date = bluetooth readline()
- print(input_data.decode())
- time.sleep(0.1)
- bluetooth.close()
- print("Done")
Explanation
In the Arduino Side, a usual process is followed. Upload the file and see in the serial monitor and the output will be displayed. In the Python side, we will have to write the code, click the Run module and see the output which will be displayed. In the Run Window, it will continue the process. When the stop Boop has come, it will stop, until then it will run and give the output.
- bluetooth.Write(b"Boop"+str.encode(str(i)))
- input-date = bluetooth readline()
- print(input_data.decode())
- time.sleep(0.1)
- bluetooth.close()
- print("Done")
In the case, they will have the Bluetooth.Write(b"Boop"+str.encode(str((i))), which needs to have byte and not unicode plus a number. In this, it will have the process to run the range, when the process is required to close, we have declared it as the bluetooth.close(). Otherwise, the connection will remain open until a timeout, which ties up the/dev/tyy.
Output
Figure 1: Output