CONTROL ARDUINO FROM PI USING GUI

To establish communication between Arduino and Raspberry pi Serial Communications can be used . The Arduino cable can be used to connect to the Raspberry pi.

REQUIREMENTS:
  1.    Raspberry pi
  2.    Arduino
  3.    Arduino cable
ARDUINO PART:
In Arduino there are two possible ways for uart communications,
 1.Using the programable port( 0 and 1)
 COMMANDS:
          Serial.begin(9600)
          Serial.read()
 2. Defining any pin as uart and using them .
 COMMANDS:
        SoftwareSerial uart1 (rxPin, txPin);
        uart1.begin(9600);
        uart1.read();

The LCD connection with Arduino is explained in my page Lcd interface with Arduino

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <LiquidCrystal.h>
char m;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
 // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
 // Print a message to the LCD.
 lcd.print("hello world!");
 delay(1000);
 Serial.begin(9600);
}
void loop() {
 if (Serial.available()) {
 m=Serial.read();
 if(m=='1'){
 //Serial.println(1);
 lcd.print("Message 1 from Raspberry pi");
 delay(1000);
 }
 else if(m=='2'){
 //Serial.println(2);
 lcd.print("Message 2 from Raspberry pi");
 delay(1000);
 }
 else if(m=='3'){
 //Serial.println(3);
 lcd.print("Message 3 from Raspberry pi");
 delay(1000);
 }
 else if(m!='1'&&m!='2'&&m!='3')
 {
 //Serial.println("Wrong no");
 lcd.print(" No message from Raspberry pi");
 delay(1000);
 }
 
 }
 }

RASPBERRY PI PART

To create GUI iam using Tkinter which can be installed in rapberry pi by using the command apt-get install python-tk.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from Tkinter import *
import serial
port =serial.Serial(
 port='/dev/ttyACM0',
 baudrate=9600,
 parity=serial.PARITY_NONE,
 stopbits=serial.STOPBITS_ONE,
 bytesize=serial.EIGHTBITS,
 )
root= Tk()
root.configure(background='bisque')
w=200
h=100
sw=root.winfo_screenwidth()
sh=root.winfo_screenheight()
x=(sw-w)/2
y=(sh-h)/2
root.geometry("%dx%d+%d+%d"%(w,h,x,y))
root.title("Mygui")
 
def c1():
 port.write("1")
 print "1"
 
def c2():
 port.write("2")
 print "2"
 
def c3():
 port.write("3")
 print "3"
 
b1=Button(root,text="LIGHT 1",command=c1,padx=5,bg='yellow')
b1.pack(side=LEFT)
b2=Button(root,text="LIGHT 2",command=c2,padx=5,bg='yellow')
b2.pack(side=LEFT)
b3=Button(root,text="LIGHT 3",command=c3,padx=5,bg='yellow')
b3.pack(side=LEFT)
 
b4=Button(root,text="Exit",command=root.destroy)
b4.place(x=70,y=75)
mainloop()

Run the program a GUI appears,

If message 1 is pressed Message 1 from Raspberry pi  will be displayed in lcd

If message 2 is pressed Message 2 from Raspberry pi  will be displayed in lcd

If message 3 is pressed Message 3 from Raspberry pi will be displayed in lcd

Thank you
SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.