Monday, May 29, 2017

Python to control RGB LED, with tkinter colorchooser/tkColorChooser

Python example to output PWM on GPIO of Raspberry Pi to control RGB LED, with tkinter GUI. Tkinter colorchooser/tkColorChooser is used to select color. Tested on Raspberry Pi 2 running Raspbian Jessie with PIXEL rel. 2017-04-10, work on both Python 2 and 3.


pyGuiPwm.py
#for Python 2
#from Tkinter import *   #for Python 2
#from tkColorChooser import askcolor

#for Python 3
from tkinter import *
from tkinter.colorchooser import *

import platform
import RPi.GPIO as GPIO

r = 0;
g = 0;
b = 0;

def on_closing():
    print("Clean up")
    pwmledR.stop()
    pwmledG.stop()
    pwmledB.stop()
    GPIO.cleanup()
    print("bye")
    master.destroy()

def getColor():
    global r, g, b
    color = askcolor(color=(r, g, b)) 
    print(color)
    rgb = color[0]
    colorVal = color[1]
    if rgb != None:
        r = rgb[0]
        g = rgb[1]
        b = rgb[2]
        print("set RGB LED")
        rVal = r/255.0
        gVal = g/255.0
        bVal = b/255.0
        print((7, gVal, bVal))
        pwmValue.set(colorVal)
        pwmledR.ChangeDutyCycle(rVal)
        pwmledG.ChangeDutyCycle(gVal)
        pwmledB.ChangeDutyCycle(bVal)

#mode = GPIO.BCM
#ledR = 16
#ledG = 20
#ledB = 21

mode = GPIO.BOARD
ledR = 36
ledG = 38
ledB = 40

print("Raspberry Pi board revision: "
      + str(GPIO.RPI_INFO['P1_REVISION']))
print("Machine: "
      + platform.machine())
print("Processor: "
      + platform.processor())
print("System: "
      + platform.system())
print("Version: "
      + platform.version())
print("Uname: "
      + str(platform.uname()))
print("Python version: "
      + platform.python_version())
print("RPi.GPIO version: "
      + str(GPIO.VERSION))

GPIO.setmode(mode)
GPIO.setup(ledR, GPIO.OUT)
GPIO.setup(ledG, GPIO.OUT)
GPIO.setup(ledB, GPIO.OUT)
pwmledR = GPIO.PWM(ledR, 50)
pwmledG = GPIO.PWM(ledG, 50)
pwmledB = GPIO.PWM(ledB, 50)
pwmledR.start(0)
pwmledG.start(0)
pwmledB.start(0)

master = Tk()

pwmValue = StringVar()
label = Label(master, textvariable=pwmValue, relief=RAISED )
label.pack()

Button(text='Select Color', command=getColor).pack()

master.protocol("WM_DELETE_WINDOW", on_closing)
mainloop()



Connection:

Python to output PWM to control LED brightness, with tkinter GUI

Python to generate PWM on GPIO of Raspberry Pi to control brightness of a LED, with tkinter GUI. Tested on Raspberry Pi 2 running Raspbian Jessie with PIXEL rel. 2017-04-10, work on both Python 2 (from Tkinter import *) and 3 (from Tkinter import * ).


pyGuiPwm.py
from Tkinter import *   #for Python 2
#from tkinter import *   #for Python 3

import platform
import RPi.GPIO as GPIO

def setPwm(newvalue):
    pwmValue.set(newvalue)
    pwmled.ChangeDutyCycle(float(newvalue))

def on_closing():
    print("Clean up")
    pwmled.stop()
    GPIO.cleanup()
    print("bye")
    master.destroy()

#mode = GPIO.BCM
#led = 21
mode = GPIO.BOARD
led = 40

print("Raspberry Pi board revision: "
      + str(GPIO.RPI_INFO['P1_REVISION']))
print("Machine: "
      + platform.machine())
print("Processor: "
      + platform.processor())
print("System: "
      + platform.system())
print("Version: "
      + platform.version())
print("Uname: "
      + str(platform.uname()))
print("Python version: "
      + platform.python_version())
print("RPi.GPIO version: "
      + str(GPIO.VERSION))

GPIO.setmode(mode)
GPIO.setup(led, GPIO.OUT)
pwmled = GPIO.PWM(led, 50)
pwmled.start(0)

master = Tk()

pwmValue = StringVar()
label = Label(master, textvariable=pwmValue, relief=RAISED )
label.pack()

slider = Scale(master, from_=0, to=100, orient=HORIZONTAL, command=setPwm)
slider.pack()

master.protocol("WM_DELETE_WINDOW", on_closing)
mainloop()



Next:
Python to control RGB LED, with tkinter colorchooser/tkColorChooser

Python to generate PWM on GPIO of Raspberry Pi


Python to generate PWM on GPIO of Raspberry Pi to control brightness of a LED. Tested on Raspberry Pi 2 running Raspbian Jessie with PIXEL rel. 2017-04-10, work on both Python 2 and 3.


pyGPIO.py
import RPi.GPIO as GPIO
import time
import platform

print("Raspberry Pi board revision: "
      + str(GPIO.RPI_INFO['P1_REVISION']))
print("Machine: "
      + platform.machine())
print("Processor: "
      + platform.processor())
print("System: "
      + platform.system())
print("Version: "
      + platform.version())
print("Uname: "
      + str(platform.uname()))
print("Python version: "
      + platform.python_version())
print("RPi.GPIO version: "
      + str(GPIO.VERSION))
print("Ctrl-C to terminate and clean up GPIO")

#mode = GPIO.BCM
#led = 21
mode = GPIO.BOARD
led = 40

GPIO.setmode(mode)
GPIO.setup(led, GPIO.OUT)
pwmled = GPIO.PWM(led, 50)
pwmled.start(0)

try:
    while True:
        for dc in range(0, 101, 5):
            pwmled.ChangeDutyCycle(dc)
            time.sleep(0.1)
        for dc in range(100, -1, -5):
            pwmled.ChangeDutyCycle(dc)
            time.sleep(0.1)
finally:
    print("Clean up")
    pwmled.stop()
    GPIO.cleanup()


Reference:
raspberry-gpio-python - Using PWM in RPi.GPIO

Connection (same as in the post "Python to control GPIO of Raspberry Pi"):


Next:
Python to output PWM to control LED brightness, with tkinter GUI

Python to get sys info on Raspberry Pi, using platform library

Python code to get system info using platform library. Work on both Python 2 and 3. Tested on Raspberry Pi 2 running Raspbian Jessie with PIXEL rel. 2017-04-10.



pySysInfo.py
import platform

print("architecture: \t\t\t"
      + str(platform.architecture()))
print("machine: \t\t\t"
      + platform.machine())
print("node: \t\t\t\t"
      + platform.node())
print("platform: \t\t\t"
      + platform.platform())
print("processor: \t\t\t"
      + platform.processor())
print("python_build: \t\t\t"
      + str(platform.python_build()))
print("python_compiler: \t\t"
      + platform.python_compiler())
print("python_branch: \t\t\t"
      + platform.python_branch())
print("python_implementation(): \t"
      + platform.python_implementation())
print("python_revision: \t\t"
      + platform.python_revision())
print("python_version: \t\t"
      + platform.python_version())
print("python_version_tuple: \t\t"
      + str(platform.python_version_tuple()))
print("release: \t\t\t"
      + platform.release())
print("system: \t\t\t"
      + platform.system())
print("version: \t\t\t"
      + platform.version())

print("Uname: \n"
      + str(platform.uname()))



Reference:
https://docs.python.org/2/library/platform.html
https://docs.python.org/3/library/platform.html


Sunday, May 28, 2017

Python to control GPIO of Raspberry Pi


It's Python example to control GPIO of Raspberry Pi, tested on Raspberry Pi 2 running Raspbian Jessie with PIXEL rel. 2017-04-10, work on both Python 2 and 3.

pyGPIO.py
import RPi.GPIO as GPIO
import time
import platform

print("Raspberry Pi board revision: "
      + str(GPIO.RPI_INFO['P1_REVISION']))
print("Python version: "
      + platform.python_version())
print("RPi.GPIO version: "
      + str(GPIO.VERSION))
print("Ctrl-C to terminate and clean up GPIO")

#mode = GPIO.BCM
#led = 21
mode = GPIO.BOARD
led = 40

GPIO.setmode(mode)
GPIO.setup(led, GPIO.OUT)

try:
    while True:
        GPIO.output(led, True)
        time.sleep(0.5)
        GPIO.output(led, False)
        time.sleep(0.5)
        GPIO.output(led, True)
        time.sleep(0.5)
        GPIO.output(led, False)
        time.sleep(2)
finally:
    print("Clean up")
    GPIO.cleanup()


Connection:


reference:
raspberry-gpio-python - RPi.GPIO module basics

Next:
Python to generate PWM on GPIO of Raspberry Pi

Wednesday, May 17, 2017

Scan Raspberry Pi IP address from network neighbor computer, by MAC

Raspberry Pi Foundation has their own range of MAC addresses B8-27-EB-00-00-00 to B8-27-EB-FF-FF-FF. You can check it at http://hwaddress.com/company/raspberry-pi-foundation



Such that we can scan neighbor with MAC address start with B8-27-EB, using arp command.

arp (stands for Address Resolution Protocol) is used to find the address of a network neighbor for a given IPv4 address.

From Linux Terminal:
$ arp -a | grep b8:27:eb

or from Windows DOS prompt (Now you can run most Linux command on Bash on Windows directly)
> arp -a | grep b8-27-eb

In the result, 192.168.1.9 is the IP address of my Raspberry Pi.




Monday, May 15, 2017

Install virtual keyboard on Raspberry Pi/Raspbian Jessie with PIXEL


To install virtual keyboard, matchbox-keyboard, on Raspberry Pi/Raspbian Jessie with PIXEL:

Always update apt-get:
$ sudo apt-get update

Install matchbox-keyboard:
$ sudo apt-get install matchbox-keyboard

Then, you can open the matchbox-keyboard by enter:
$ matchbox-keyboard

This video show how to install matchbox-keyboard on Raspberry Pi 3/Raspbian Jessie with PIXEL (2017-04-10).

Thursday, May 11, 2017

Waveshare 4" 800x480 HDMI IPS Resistive Touch Screen, for Raspberry Pi

It's a 4" 800x480 HDMI IPS display with resistive touch screen, support Raspberry Pi. Product page in Chinese: http://www.waveshare.net/wiki/4inch_HDMI_LCD


It's the Open Box video, with first setup on Raspberry Pi 3/Raspbian Jessie with Pixel release 2017-04-10.



CAUTION: If you have problem on mis-touch detection when using on Raspberry Pi with new Raspbian Jessie after 2017-03-02, most probably you install the old driver come in the bundle CD-ROM. As show in the video below. You are suggested to download the update driver from the product page.


The touch calibration program, xinput_calibrator, can be download from the product page.
- Download Xinput-calibrator_0.7.5-1_armhf
- install:
$ sudo dpkg -i -B xinput-calibrator_0.7.5-1_armhf.deb


Updated@2020-05-24

Found the product page with install guide, refer https://www.waveshare.com/wiki/4inch_HDMI_LCD.

Checked at 2020-05, with updated Raspbian Buster with desktop on Raspberry Pi 3. It's worked.




Related:
Python to control Raspberry Pi Camera - run on Raspberry Pi 3 with Waveshare 4" HDMI LCD, run it on Android phone with Remote Desktop app. The local mounted display (4" HDMI LCD) is used as viewfinder.

Sunday, May 7, 2017

Try RASPBIAN JESSIE WITH PIXEL on x86 PC using VirtualBox/Windows 10


This video show how to download RASPBIAN JESSIE WITH PIXEL for x86, and install on Windows 10 with Oracle VirtualBox.


pixel_x86 image iso can be download here:
http://downloads.raspberrypi.org/pixel_x86/images/

Saturday, May 6, 2017

Introducing AIY Projects: DIY AI for Makers (Voice Kit)

AIY Projects is bringing do-it-yourself artificial intelligence to the Maker community. Go behind the scenes with James, AIY Projects engineer, as he assembles the Voice Kit and uses it to control his own model train. We can’t wait to see the awesome and unexpected ways you will use voice in your own projects.

For more info on AIY Projects and the Voice Kit visit https://aiyprojects.withgoogle.com/.