<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://w.electrodragon.com/w/index.php?action=history&amp;feed=atom&amp;title=RPI_Python</id>
	<title>RPI Python - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://w.electrodragon.com/w/index.php?action=history&amp;feed=atom&amp;title=RPI_Python"/>
	<link rel="alternate" type="text/html" href="https://w.electrodragon.com/w/index.php?title=RPI_Python&amp;action=history"/>
	<updated>2026-07-21T20:02:48Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.2</generator>
	<entry>
		<id>https://w.electrodragon.com/w/index.php?title=RPI_Python&amp;diff=31429&amp;oldid=prev</id>
		<title>Chao at 03:58, 27 May 2021</title>
		<link rel="alternate" type="text/html" href="https://w.electrodragon.com/w/index.php?title=RPI_Python&amp;diff=31429&amp;oldid=prev"/>
		<updated>2021-05-27T03:58:59Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Python ==&lt;br /&gt;
=== Blinking LED ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
import time&lt;br /&gt;
import RPi.GPIO as GPIO&lt;br /&gt;
LED = 4               #Physical pin 7, BCM pin GPIO.4 on the BCM2835&lt;br /&gt;
GPIO.setmode(GPIO.BCM)&lt;br /&gt;
GPIO.setup(LED, GPIO.OUT)&lt;br /&gt;
while 1:&lt;br /&gt;
GPIO.output(LED, True)&lt;br /&gt;
time.sleep(0.5)&lt;br /&gt;
GPIO.output(LED, False)&lt;br /&gt;
time.sleep(0.5)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Using NRF24L01 ===&lt;br /&gt;
==== Arduino Side ====&lt;br /&gt;
* Download the [https://github.com/stanleyseow/RF24 RF24 library], turn on the arduino IDE, in the example sketches, choose pinpair.&lt;br /&gt;
* Wire up your arduino to radio, and make one more wire from digital pin 7 to GND, this is to send the radio to a transmitter. &lt;br /&gt;
* replace the loop part, because the RPI code can not read unsigned long data unless you change it.So for demostration, we only send bytes data to RPI as below, you can see here until word &amp;quot;d&amp;quot; in &amp;quot;doing&amp;quot;, the length is 32 bytes.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Arduino&amp;quot;&amp;gt; &lt;br /&gt;
void loop(void)&lt;br /&gt;
{&lt;br /&gt;
    // transmit and increment the counter&lt;br /&gt;
    radio.write(&amp;quot;Hi, Electrodragon, How are you doing?&amp;quot;, 32);&lt;br /&gt;
    // pause a second&lt;br /&gt;
    delay(1000);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
==== RPI side ====&lt;br /&gt;
===== RPI initiation =====&lt;br /&gt;
* turn on the SSH and sudo firstly&lt;br /&gt;
 sudo su&lt;br /&gt;
* Load SPI for RPI&lt;br /&gt;
 modprobe spi_bcm2708&lt;br /&gt;
 modprobe spidev&lt;br /&gt;
===== Downloading demo sketch and library =====&lt;br /&gt;
* turn the following commands (make sure installed git):&lt;br /&gt;
 git clone https://github.com/stanleyseow/RF24.git&lt;br /&gt;
 cd RF24/librf24-rpi/librf24/examples/&lt;br /&gt;
 make&lt;br /&gt;
* (optional) you can move the librf24 subfolder out from the main folder, since it is the only folder we will use here, so you can do&lt;br /&gt;
 cd RF/librf24-rpi&lt;br /&gt;
 mv librf24 /home/pi // move to the pi default folder&lt;br /&gt;
 rm librf24 rf // rename it to rf&lt;br /&gt;
===== Editing demo sketch rpi-hub.cpp =====&lt;br /&gt;
navigate to examples folder in librf24 folder&lt;br /&gt;
 cd examples&lt;br /&gt;
 nano rpi-hub.cpp&lt;br /&gt;
for the demo sketch, please change (optional), comment the following settings, this is not very necessary.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:RPI-RF.png|400px]]&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now the final step is complie it&lt;br /&gt;
 make&lt;br /&gt;
===== Running, reference =====&lt;br /&gt;
* run the bash command, now you can see the data printing on&lt;br /&gt;
 ./rpi-hub&lt;br /&gt;
* [http://www.homautomation.org/2014/06/11/communicating-from-low-power-arduino-to-raspberry-pi-via-nrf24l01/ reference link]&lt;br /&gt;
* Demo video (not yet updated)&lt;br /&gt;
=== Display: 1602 LCD ===&lt;br /&gt;
* Install the python necessary packages, see the next section&lt;br /&gt;
* wiring the pi to the 1602 LCD as below&lt;br /&gt;
[[File:Raspberry pi 1602.png|400px]]&lt;br /&gt;
* Get the python library by command: &lt;br /&gt;
 git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git&lt;br /&gt;
* If you're using a Version 2 Raspberry Pi, pin #21 has been replaced with pin #27 so edit Adafruit_CharLCD.py and change:&lt;br /&gt;
 def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None): &lt;br /&gt;
to &lt;br /&gt;
 def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 27, 22], GPIO = None):&lt;br /&gt;
* Run the testing code&lt;br /&gt;
 chmod +x Adafruit_CharLCD.py&lt;br /&gt;
 sudo ./Adafruit_CharLCD.py&lt;br /&gt;
* One more example to display the ethernet IP address: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
&lt;br /&gt;
from Adafruit_CharLCD import Adafruit_CharLCD&lt;br /&gt;
from subprocess import *&lt;br /&gt;
from time import sleep, strftime&lt;br /&gt;
from datetime import datetime&lt;br /&gt;
&lt;br /&gt;
lcd = Adafruit_CharLCD()&lt;br /&gt;
&lt;br /&gt;
cmd = &amp;quot;ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
lcd.begin(16,1)&lt;br /&gt;
&lt;br /&gt;
def run_cmd(cmd):&lt;br /&gt;
        p = Popen(cmd, shell=True, stdout=PIPE)&lt;br /&gt;
        output = p.communicate()[0]&lt;br /&gt;
        return output&lt;br /&gt;
&lt;br /&gt;
while 1:&lt;br /&gt;
        lcd.clear()&lt;br /&gt;
        ipaddr = run_cmd(cmd)&lt;br /&gt;
        lcd.message(datetime.now().strftime('%b %d  %H:%M:%S\n'))&lt;br /&gt;
        lcd.message('IP %s' % ( ipaddr ) )&lt;br /&gt;
        sleep(2)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Driving Stepper Motor (using IC LD293D)===&lt;br /&gt;
[[File:Learn raspberry pi breadboard 293.png|thumbnail]]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
import RPi.GPIO as GPIO&lt;br /&gt;
import time&lt;br /&gt;
&lt;br /&gt;
GPIO.setmode(GPIO.BCM)&lt;br /&gt;
&lt;br /&gt;
enable_pin = 18&lt;br /&gt;
coil_A_1_pin = 4&lt;br /&gt;
coil_A_2_pin = 17&lt;br /&gt;
coil_B_1_pin = 23&lt;br /&gt;
coil_B_2_pin = 24&lt;br /&gt;
&lt;br /&gt;
GPIO.setup(enable_pin, GPIO.OUT)&lt;br /&gt;
GPIO.setup(coil_A_1_pin, GPIO.OUT)&lt;br /&gt;
GPIO.setup(coil_A_2_pin, GPIO.OUT)&lt;br /&gt;
GPIO.setup(coil_B_1_pin, GPIO.OUT)&lt;br /&gt;
GPIO.setup(coil_B_2_pin, GPIO.OUT)&lt;br /&gt;
&lt;br /&gt;
GPIO.output(enable_pin, 1)&lt;br /&gt;
&lt;br /&gt;
def forward(delay, steps):  &lt;br /&gt;
  for i in range(0, steps):&lt;br /&gt;
    setStep(1, 0, 1, 0)&lt;br /&gt;
    time.sleep(delay)&lt;br /&gt;
    setStep(0, 1, 1, 0)&lt;br /&gt;
    time.sleep(delay)&lt;br /&gt;
    setStep(0, 1, 0, 1)&lt;br /&gt;
    time.sleep(delay)&lt;br /&gt;
    setStep(1, 0, 0, 1)&lt;br /&gt;
    time.sleep(delay)&lt;br /&gt;
&lt;br /&gt;
def backwards(delay, steps):  &lt;br /&gt;
  for i in range(0, steps):&lt;br /&gt;
    setStep(1, 0, 0, 1)&lt;br /&gt;
    time.sleep(delay)&lt;br /&gt;
    setStep(0, 1, 0, 1)&lt;br /&gt;
    time.sleep(delay)&lt;br /&gt;
    setStep(0, 1, 1, 0)&lt;br /&gt;
    time.sleep(delay)&lt;br /&gt;
    setStep(1, 0, 1, 0)&lt;br /&gt;
    time.sleep(delay)&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
def setStep(w1, w2, w3, w4):&lt;br /&gt;
  GPIO.output(coil_A_1_pin, w1)&lt;br /&gt;
  GPIO.output(coil_A_2_pin, w2)&lt;br /&gt;
  GPIO.output(coil_B_1_pin, w3)&lt;br /&gt;
  GPIO.output(coil_B_2_pin, w4)&lt;br /&gt;
&lt;br /&gt;
while True:&lt;br /&gt;
  delay = raw_input(&amp;quot;Delay between steps (milliseconds)?&amp;quot;)&lt;br /&gt;
  steps = raw_input(&amp;quot;How many steps forward? &amp;quot;)&lt;br /&gt;
  forward(int(delay) / 1000.0, int(steps))&lt;br /&gt;
  steps = raw_input(&amp;quot;How many steps backwards? &amp;quot;)&lt;br /&gt;
  backwards(int(delay) / 1000.0, int(steps))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Driving DC Motor, Servo using L293D ===&lt;br /&gt;
====direct soft GIPO.PWM control for servo&lt;br /&gt;
* Use soft GPIO.PWM control servo, and GUI interface, wiring the servo as image here, a 1K resistor added between the RPI IO and servo signal pin(for limiting current and protect RPI GPIO).&lt;br /&gt;
[[File:Rpck 1001.png|thumbnail]]&lt;br /&gt;
* check the demo code on GUI section belwo (also comments added). &lt;br /&gt;
* It is pretty simple one IO control.(reference link: http://razzpisampler.oreilly.com/ch05.html)&lt;br /&gt;
==== Other methods ====&lt;br /&gt;
* Adafruit has other method but need to install new OS.&lt;br /&gt;
* Using raspirobot board, see the [https://github.com/simonmonk/raspirobotboard/ tutorial here]&lt;br /&gt;
&lt;br /&gt;
=== Installing Python packages ===&lt;br /&gt;
 sudo apt-get install python-dev (may skip)&lt;br /&gt;
 sudo apt-get install python-setuptools (may skip)&lt;br /&gt;
 sudo easy_install -U distribute (may skip)&lt;br /&gt;
 sudo apt-get install python-pip&lt;br /&gt;
 sudo pip install rpi.gpio&lt;br /&gt;
 sudo apt-get install python-serial (Install the serial terminal)&lt;br /&gt;
* type python to enter into python command mode.&lt;br /&gt;
==== Python GUI programming on RPI ====&lt;br /&gt;
It is no need to install Tkinter on RPI, type following command to test&lt;br /&gt;
 python // to enter into python IDLE&lt;br /&gt;
 import Tkinter (T is cap on 2.6 version, but t in new 3.0+ version)&lt;br /&gt;
If shows error that module is not installed, run install&lt;br /&gt;
 sudo apt-get install python-tk&lt;br /&gt;
&lt;br /&gt;
==== Demo code to run soft PWM control for servo ====&lt;br /&gt;
use nano to edit, copy and paste the following code, save this file at /home/pi/x.py&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt; &lt;br /&gt;
from Tkinter import *&lt;br /&gt;
import RPi.GPIO as GPIO&lt;br /&gt;
import time&lt;br /&gt;
&lt;br /&gt;
GPIO.setmode(GPIO.BCM)&lt;br /&gt;
GPIO.setup(18, GPIO.OUT)&lt;br /&gt;
pwm = GPIO.PWM(18, 100) // where 18 is the channel GPIO, and 100 is the hz&lt;br /&gt;
pwm.start(5) // 5 is the duty at start&lt;br /&gt;
&lt;br /&gt;
class App:&lt;br /&gt;
&lt;br /&gt;
    def __init__(self, master):&lt;br /&gt;
        frame = Frame(master)&lt;br /&gt;
        frame.pack()&lt;br /&gt;
        scale = Scale(frame, from_=0, to=180, &lt;br /&gt;
              orient=HORIZONTAL, command=self.update) // it can scroll from 0 to 180, orientation is horizontal, and run the next function self.update&lt;br /&gt;
        scale.grid(row=0)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    def update(self, angle):&lt;br /&gt;
        duty = float(angle) / 10.0 + 2.5 // calculate the duty&lt;br /&gt;
        pwm.ChangeDutyCycle(duty) // then change the duty&lt;br /&gt;
&lt;br /&gt;
root = Tk()&lt;br /&gt;
root.wm_title('Servo Control')&lt;br /&gt;
app = App(root)&lt;br /&gt;
root.geometry(&amp;quot;200x50+0+0&amp;quot;) // set the windows size 200x50, and initiated position 0 and 0&lt;br /&gt;
root.mainloop()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Run the test ====&lt;br /&gt;
Turn on VNC for RPI, let the VNC choose automatic resolution, open LXterminal, and run the following command&lt;br /&gt;
gksudo python /home/pi/x.py&lt;br /&gt;
[[File:Tkinter.png|thumbnail]]&lt;br /&gt;
&lt;br /&gt;
=== RPI with IIC and using MCP2300xx as IO expander ===&lt;br /&gt;
==== Installing IIC ====&lt;br /&gt;
* '''The following setup can be done in advance setting of raspi-config, just run sudo raspi-config'''&lt;br /&gt;
* The new Raspbian distro already have the I2C driver installed but they are disabled by default. To enable it all you need to do is comment out a line by putting # in front. At the prompt type: sudo nano /etc/modprobe.d/raspi-blacklist.conf&amp;lt;br /&amp;gt;&lt;br /&gt;
then add a # on the 3rd line.&amp;lt;br /&amp;gt;&lt;br /&gt;
* Next edit the modules file by: sudo nano /etc/modules&lt;br /&gt;
Add i2c-dev to a new line.&amp;lt;br /&amp;gt;&lt;br /&gt;
* Now install the i2c-tools package by: sudo apt-get install i2c-tools&lt;br /&gt;
* Now add a new user to the i2c group: sudo adduser pi i2c&lt;br /&gt;
If your board is the Rev 2 type this: sudo i2cdetect -y 1&lt;br /&gt;
You should see something like this:&amp;lt;br /&amp;gt;&lt;br /&gt;
[[File:RPIIIC address.png]]&amp;lt;br /&amp;gt;&lt;br /&gt;
in the output, row on &amp;quot;0&amp;quot;, and column &amp;quot;20&amp;quot; means the IIC address is 0x20&lt;br /&gt;
* Next install the  python-smbus python module: sudo apt-get install python-smbus &lt;br /&gt;
Now you are ready to use the i2c with python.&lt;br /&gt;
* See the example using [[RPI relay shield]] page, board using MCP23008 as an expander.&lt;br /&gt;
&lt;br /&gt;
=== Python Programming ===&lt;br /&gt;
* Turn IO 7 ON&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
import RPi.GPIO as GPIO ## Import GPIO library&lt;br /&gt;
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering&lt;br /&gt;
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT&lt;br /&gt;
GPIO.output(7,True) ## Turn on GPIO pin 7 &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Blink LED&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
import RPi.GPIO as GPIO ## Import GPIO library&lt;br /&gt;
import time ## Import 'time' library. Allows us to use 'sleep'&lt;br /&gt;
&lt;br /&gt;
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering&lt;br /&gt;
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT&lt;br /&gt;
&lt;br /&gt;
## Define a function named Blink()&lt;br /&gt;
def Blink(numTimes,speed):&lt;br /&gt;
for i in range(0,numTimes):## Run loop numTimes&lt;br /&gt;
print &amp;quot;Iteration &amp;quot; + str(i+1)## Print current loop&lt;br /&gt;
GPIO.output(7,True)## Switch on pin 7&lt;br /&gt;
time.sleep(speed)## Wait&lt;br /&gt;
GPIO.output(7,False)## Switch off pin 7&lt;br /&gt;
time.sleep(speed)## Wait&lt;br /&gt;
print &amp;quot;Done&amp;quot; ## When loop is complete, print &amp;quot;Done&amp;quot;&lt;br /&gt;
GPIO.cleanup()&lt;br /&gt;
&lt;br /&gt;
## Ask user for total number of blinks and length of each blink&lt;br /&gt;
iterations = raw_input(&amp;quot;Enter total number of times to blink: &amp;quot;)&lt;br /&gt;
speed = raw_input(&amp;quot;Enter length of each blink(seconds): &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## Start Blink() function. Convert user input from strings to numeric data types and pass to Blink() as parameters&lt;br /&gt;
Blink(int(iterations),float(speed))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[category: Raspberry Pi]]&lt;br /&gt;
&lt;br /&gt;
[[category: Python]]&lt;/div&gt;</summary>
		<author><name>Chao</name></author>
	</entry>
</feed>