mirror of https://github.com/joan2937/pigpio
206 lines
8.0 KiB
HTML
206 lines
8.0 KiB
HTML
|
|
||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
||
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||
|
<head>
|
||
|
<meta name="description" content="Raspberry Pi Reg. C GPIO library and Python GPIO module and shell command utilities to control the GPIO, including SPI, I2C, and serial links." />
|
||
|
<meta name="keywords" content="raspberry, pi, C, Python, GPIO, library, shell, command, utilities, module, SPI, I2C, serial" />
|
||
|
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
|
||
|
<title>pigpio library</title>
|
||
|
<link rel="stylesheet" type="text/css" href="scripts/index.css">
|
||
|
<link rel="icon" href="favicon.ico" type="image/x-icon">
|
||
|
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
<table style="padding:0px; border:0px; margin:0px; width:780px; background-color:#e0e0e0;">
|
||
|
<td style="background:#EAF2E6 url('images/sidebar.gif') repeat-y; width:35px; height:100%"></td>
|
||
|
<td>
|
||
|
<table>
|
||
|
<div style="background:url('images/topbar.gif') repeat-x; height: 70px; font-size:1.5em; vertical-align: top;"><a href="index.html"><img src="images/pigpio-logo.gif" border="0" /></a>pigpio library</div>
|
||
|
</table>
|
||
|
<table><div>
|
||
|
<td><img src="images/keypad.jpg" width="250"></td>
|
||
|
<td><img src="images/ldr-cap.jpg" width="250"></td>
|
||
|
<td><img src="images/meter.jpg" width="250"></td>
|
||
|
</div></table>
|
||
|
<table>
|
||
|
<td style="vertical-align: top; background-color: #98bf21;"><a class="l1" href="index.html">pigpio</a>
|
||
|
<a class="l1" href="cif.html">pigpio C I/F</a>
|
||
|
<a class="l1" href="pigpiod.html">pigpiod</a>
|
||
|
<a class="l1" href="pdif2.html">pigpiod C I/F</a>
|
||
|
<a class="l1" href="python.html">Python</a>
|
||
|
<a class="l1" href="pigs.html">pigs</a>
|
||
|
<a class="l1" href="piscope.html">piscope</a>
|
||
|
<a class="l1" href="misc.html">Misc</a>
|
||
|
<a class="l1" href="examples.html">Examples</a>
|
||
|
<a class="l1" href="download.html">Download</a>
|
||
|
<a class="l1" href="faq.html">FAQ</a>
|
||
|
<a class="l1" href="sitemap.html">Site Map</a>
|
||
|
</td>
|
||
|
<td><center><h2>LDR Example</h2></center>
|
||
|
<p>The following code shows a method of reading analogue sensors on
|
||
|
the digital input only Pi. A Light Dependent Resistor (LDR)
|
||
|
varies its resistance according to the incident light
|
||
|
intensisty.</p>
|
||
|
<h3>SETUP</h3>
|
||
|
<img src="images/LDR-fritz.png" alt="fritzing diagram" style=
|
||
|
"width: 200px; height: 300px;" align="left" hspace="10">The LDR
|
||
|
used is a Cadmium Sulphide device with a 1MOhm dark resistance and
|
||
|
2-4KOhm at 100 lux. The capacitor is a 104
|
||
|
ceramic.<span itemprop="name"><br>
|
||
|
<br>
|
||
|
One end of the capacitor is connected to Pi ground.<br>
|
||
|
<br>
|
||
|
One end of the LDR is connected to Pi 3V3.<br>
|
||
|
<br>
|
||
|
The other ends of the capacitor and LDR are connected to a spare
|
||
|
gpio.</span><br>
|
||
|
<p>Here P1-1 is used for 3V3, P1-20 is used for ground, and gpio 18
|
||
|
(P1-12) is used for the gpio.<br clear="all"></p>
|
||
|
<p><img src="images/LDR-photo.jpg" style=
|
||
|
"width: 500px; height: 667px;" alt="photo of set-up"></p>
|
||
|
<h3>CODE</h3>
|
||
|
<code>#include <stdio.h><br>
|
||
|
<br>
|
||
|
#include <pigpio.h><br>
|
||
|
<br>
|
||
|
/*
|
||
|
-----------------------------------------------------------------------<br>
|
||
|
|
||
|
<br>
|
||
|
3V3 ----- Light Dependent Resistor --+-- Capacitor
|
||
|
----- Ground<br>
|
||
|
|
||
|
|<br>
|
||
|
|
||
|
+-- gpio<br>
|
||
|
<br>
|
||
|
<br>
|
||
|
cc -o LDR LDR.c -lpigpio -lpthread -lrt<br>
|
||
|
sudo ./LDR<br>
|
||
|
<br>
|
||
|
*/<br>
|
||
|
<br>
|
||
|
#define LDR 18<br>
|
||
|
<br>
|
||
|
/* forward declaration */<br>
|
||
|
<br>
|
||
|
void alert(int pin, int level, uint32_t tick);<br>
|
||
|
<br>
|
||
|
int main (int argc, char *argv[])<br>
|
||
|
{<br>
|
||
|
if (gpioInitialise()<0) return 1;<br>
|
||
|
<br>
|
||
|
gpioSetAlertFunc(LDR, alert); /* call alert when LDR
|
||
|
changes state */<br>
|
||
|
<br>
|
||
|
while (1)<br>
|
||
|
{<br>
|
||
|
gpioSetMode(LDR, PI_OUTPUT); /*
|
||
|
drain capacitor */<br>
|
||
|
<br>
|
||
|
gpioWrite(LDR, PI_OFF);<br>
|
||
|
<br>
|
||
|
gpioDelay(200); /* 50 micros is
|
||
|
enough, 200 is overkill */<br>
|
||
|
<br>
|
||
|
gpioSetMode(LDR, PI_INPUT); /* start
|
||
|
capacitor recharge */<br>
|
||
|
<br>
|
||
|
gpioDelay(10000); /* nominal 100
|
||
|
readings per second */<br>
|
||
|
}<br>
|
||
|
<br>
|
||
|
gpioTerminate();<br>
|
||
|
}<br>
|
||
|
<br>
|
||
|
void alert(int pin, int level, uint32_t tick)<br>
|
||
|
{<br>
|
||
|
static uint32_t inited = 0;<br>
|
||
|
static uint32_t lastTick, firstTick;<br>
|
||
|
<br>
|
||
|
uint32_t diffTick;<br>
|
||
|
<br>
|
||
|
if (inited)<br>
|
||
|
{<br>
|
||
|
diffTick = tick - lastTick;<br>
|
||
|
lastTick = tick;<br>
|
||
|
<br>
|
||
|
if (level == 1) printf("%u %d\ ",
|
||
|
tick-firstTick, diffTick);<br>
|
||
|
}<br>
|
||
|
else<br>
|
||
|
{<br>
|
||
|
inited = 1;<br>
|
||
|
firstTick = tick;<br>
|
||
|
lastTick = firstTick;<br>
|
||
|
}<br>
|
||
|
}<br></code>
|
||
|
<h3>BUILD</h3>
|
||
|
<code>cc -o LDR LDR.c -lpigpio -lrt -lpthread<br></code>
|
||
|
<h3>RUN</h3>
|
||
|
<code>sudo ./LDR >LDR.dat &</code><br>
|
||
|
<br>
|
||
|
While the program is running you can capture the waveform using the
|
||
|
notification feature built in to pigpio. Issue the following
|
||
|
commands on the Pi.<br>
|
||
|
<br>
|
||
|
<code>pigs no<br>
|
||
|
pig2vcd </dev/pigpio0 >LDR.vcd &<br>
|
||
|
pigs nb 0 0x40000 # set bit for gpio 18<br></code>
|
||
|
<p>Change the light falling on the LDR for a few seconds (e.g.
|
||
|
shine a torch on it or shade it with your hands).<br></p>
|
||
|
<code>pigs nc 0</code><br>
|
||
|
<p>The file LDR.vcd will contain the captured waveform, which can
|
||
|
be viewed using GTKWave.</p>
|
||
|
<p>Overview</p>
|
||
|
<img src="images/LDR-wave-1.png" style=
|
||
|
"width: 600px; height: 100px;" alt="LDR waveform 1"><br>
|
||
|
<p>Reading circa every 10ms<br></p>
|
||
|
<img src="images/LDR-wave-2.png" style=
|
||
|
"width: 600px; height: 100px;" alt="LDR waveform 2"><br>
|
||
|
<p>One reading, circa 400us<br></p>
|
||
|
<img src="images/LDR-wave-3.png" style=
|
||
|
"width: 600px; height: 100px;" alt="LDR waveform 3"><br>
|
||
|
<p>The file LDR.dat will contain pairs of timestamps and recharge
|
||
|
time (in us). The following script will convert the
|
||
|
timestamps into seconds.<span style=
|
||
|
"font-style: italic;"><br></span></p>
|
||
|
<p><code>awk '{print $1/1000000, $2}' LDR.dat
|
||
|
>LDR-secs.dat</code></p>
|
||
|
<p>Gnuplot is a useful tool to graph data.<br></p>
|
||
|
plot [14:24] 'LDR-secs.dat' with lines title 'LDR'
|
||
|
<p>Gnuplot readings 14-24 seconds<br></p>
|
||
|
<p><img src="images/LDR-gnup-1.png" style=
|
||
|
"width: 600px; height: 321px;" alt="gnuplot 1"></p>
|
||
|
plot [18:21] 'LDR-secs.dat' with lines title 'LDR'<br>
|
||
|
<br>
|
||
|
Gnuplot readings 18-21 seconds
|
||
|
<p><img src="images/LDR-gnup-2.png" style=
|
||
|
"width: 600px; height: 321px;" alt="Gnuplot 2"></p>
|
||
|
</td>
|
||
|
</table>
|
||
|
<div style="vertical-align: center; text-align: center; background-color:#98bf21; font-size:0.8em; height:30px"><a class="l2" href="index.html">[pigpio]</a>
|
||
|
<a class="l2" href="cif.html">[pigpio C I/F]</a>
|
||
|
<a class="l2" href="pigpiod.html">[pigpiod]</a>
|
||
|
<a class="l2" href="pdif2.html">[pigpiod C I/F]</a>
|
||
|
<a class="l2" href="python.html">[Python]</a>
|
||
|
<a class="l2" href="pigs.html">[pigs]</a>
|
||
|
<a class="l2" href="piscope.html">[piscope]</a>
|
||
|
<a class="l2" href="misc.html">[Misc]</a>
|
||
|
<a class="l2" href="examples.html">[Examples]</a>
|
||
|
<a class="l2" href="download.html">[Download]</a>
|
||
|
<a class="l2" href="faq.html">[FAQ]</a>
|
||
|
<a class="l2" href="sitemap.html">[Site Map]</a>
|
||
|
</div>
|
||
|
<table><tr>
|
||
|
<td style="width: 200px"><div style="text-align: left;"><small>© 2012-2020</small></div></td>
|
||
|
<td style="width: 350px"><div style="text-align: center;">e-mail: pigpio @ abyz.me.uk</div></td>
|
||
|
<td style="width: 200px"><div style="text-align: right;"><small>Updated: 29/04/2020</small></div></td>
|
||
|
</tr></table>
|
||
|
</td>
|
||
|
</table>
|
||
|
</body>
|
||
|
</html>
|