Bogdan F
Published

RGB Led Control With Arduino And Python Tkinter

A little program made with an RGB led. The Python code and Arduino code is available. I will a make a video soon. Thank you!

IntermediateWork in progress1 hour7,300
RGB Led Control With Arduino And Python Tkinter

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Resistor 220 ohm
Resistor 220 ohm
×3
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Command prompt
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Arduino Sketch for RGB Led

Thank you! Have a lovely day :)

Code

Python Tkinter Code

Python
Use this file to acces the user interface
# hope you enjoy it and Merry X-mas :)

import serial
import tkinter
import tkinter.messagebox

arduinoData = serial.Serial('com7',9600)
def led_rosu():
	arduinoData.write(b'0')

def led_verde():
	arduinoData.write(b'1')

def led_albas():
	arduinoData.write(b'2')

def led_alb():
	arduinoData.write(b'3') 

def led_vio():
	arduinoData.write(b'4')

def led_random():
	arduinoData.write(b'5')

# MAIN
ledrgb = tkinter.Tk()
ledrgb.title("Arduino RGB Led Control")
ledrgb.configure(background="grey")
tkinter.messagebox.showinfo("X","!!! Bun venit !!!")

#exit function
def close_window():
	arduinoData.write(b'0')
	ledrgb.destroy()
	exit()


Button = tkinter.Button

#just a few buttons

btn1 = Button(ledrgb, text="Rosu",bg="red",fg="white",command = led_rosu)
btn2 = Button(ledrgb, text="Verde",bg="red",fg="white",command = led_verde)
btn3 = Button(ledrgb, text="Albastru",bg="red",fg="white",command = led_albas)
btn4 = Button(ledrgb, text="Alb", command = led_alb)
btn5 = Button(ledrgb, text="Violet", command = led_vio)
btn6 = Button(ledrgb, text="Random", command=led_random)
btn7 = Button(ledrgb, text="Exit", command=close_window) .grid(row=3)

btn1.grid(row=0,column=1)
btn2.grid(row=0,column=2)
btn3.grid(row=1,column=1)
btn4.grid(row=1,column=2)
btn5.grid(row=2,column=1)
btn6.grid(row=2,column=2)

#good luck
ledrgb.mainloop()

Arduino Rgb Control

Arduino
Upload this code to Arduino Uno before running the .py file
// FeBe noi.2018
// control led rgb prin python tkinter
// fisierul rgb_buc.py

char  serialData;

//check always the pins
int redPin= 7;
int greenPin = 6;
int bluePin = 5;
    
void setup() 
  {
      pinMode(redPin, OUTPUT);
      pinMode(greenPin, OUTPUT);
      pinMode(bluePin, OUTPUT);
      Serial.begin(9600);
    }
    
void loop() {

  int valoareIluminare = analogRead(0);
  Serial.println(valoareIluminare, DEC); 
  delay(100);
/*
      for (int i = 0 ; i < 256; i++) {
        setColor(i, i, 0);
        delay(100);
      }
      
      setColor(0, 0, 255); // Blue Color
      delay(1000);
      
      delay(500);
      
      delay(1000);
      setColor(100, 100, 100);
      delay(1500);
      setColor(100,0,200);
      delay(2000);
    */

    if (Serial.available() > 0) {
      serialData = Serial.read();
      Serial.print(serialData);

      if(serialData == '0') {
        setColor(255, 0, 0); // Red Color 
        } else if (serialData == '1') {
        setColor(0, 255, 0); // Green Color 
        } else if (serialData == '2') {
        setColor(0, 0, 255); // Blue Color
        } else if (serialData == '3') {
        setColor(255, 255, 255); // White Color
        } else if (serialData == '4') {
        setColor(170, 0, 255); // Purple Color 
        } else if (serialData == '5') {
       // int rint = random(1,255);
       // int rrnt = random(1,255);
       // int rtnt = random(1,255);
       // for (int i = 255; i>0; i--){
        //  setColor(0, i, 0);
        //  delay(5);
        //  }
//        setColor(rint, rrnt, rtnt);
       /* delay(2000);
        setColor(rrnt, rtnt, rtnt);
        }*/
    }
}
}
//rgb 
void setColor(int redValue, int greenValue, int blueValue) {
      analogWrite(redPin, redValue);
      analogWrite(greenPin, greenValue);
      analogWrite(bluePin, blueValue);
    }

Credits

Bogdan F

Bogdan F

6 projects • 7 followers
Welcome to my profile. I’m currently diving into Arduino and the Tkinter library. http://buymeacoffee.com/bogdanf

Comments