Connecting The BT And Motor With Arduino And Python

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
  1. #include " Softwareserial.h"  
  2. softwareSerial serial.connection(10, 11);#  
  3. define BUFFER_SIZE 64  
  4. char inData[BUFFER_SIZE];  
  5. char inchar = -1;  
  6. int count = 0;  
  7. int i = 0;  
  8. void setup()  
  9. {  
  10.     serial.begin(9600);  
  11.     serial_connection.begin(9600);  
  12.     serial_connection.println("Ready!!!");  
  13.     serial_println("Started");  
  14. }  
  15. Void loop()  
  16. {  
  17.     byte byte = Count = serial_connection.available();  
  18.     if (byte_ Count) {  
  19.         Serial println("Incoming Data");  
  20.         Int first_bytes = bytes - Count;  
  21.         int remaining_bytes = 0;  
  22.         if (first_bytes > +BUFFER_SIZE - 1)  
  23.         {  
  24.             remaining_bytes = bytes_count = (BUFFER_SIZE - 1);  
  25.         }  
  26.         for (i = 0; i < first_byte; i++)  
  27.         {  
  28.             intchar = Serial_connection.read();  
  29.             int Data[i] = inchar;  
  30.         }  
  31.         intData[i] = '\0';  
  32.         if (string(inData) == "BOOP 2")  
  33.         {  
  34.             Serial.println("************* start Motor ********************");  
  35.         }  
  36.         elseif(string(inData) == "BOOP 4")  
  37.         {  
  38.             Serial.println("************* stop Motor ********************");  
  39.         }  
  40.         for (i = 0; i < remaining_bytes; i++)  
  41.         {  
  42.             inchar = Serial_connection.read();  
  43.         }  
  44.         Serial_ConnectionPrintln("Hello From Blue" + string(count));  
  45.         Count++;  
  46.     }  
  47.     delay(100)  
Programming For Python Part
  1. import serial  
  2. import time  
  3. Print ("Start")  
  4. port="/dev/tty.HC-06-DevB"  
  5. bluetooth=serial.serial(port,9600)  
  6. print("Connected"  
  7. bluetooth.flushInput()  
  8. for i in range(5)  
  9. print("ping")  
  10. bluetooth.Write(b"Boop"+str.encode(str(i)))  
  11. input-date = bluetooth readline()  
  12. print(input_data.decode())  
  13. time.sleep(0.1)  
  14. bluetooth.close()  
  15. 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.
  1. bluetooth.Write(b"Boop"+str.encode(str(i)))  
  2. input-date = bluetooth readline()  
  3. print(input_data.decode())  
  4. time.sleep(0.1)  
  5. bluetooth.close()  
  6. 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  

Up Next
    Ebook Download
    View all
    Learn
    View all