py-arduino access Arduino from Python

Documents

How to install py-arduino
Common problems
py_arduino.cli.*: How to use
py_arduino.cli.*: Reference

Examples - Low level

Temperature sensing (LM35)

Examples - Web Application

TODO

---

View the Project on GitHub hgdeoro/py-arduino

Sensing temperature with LM35

What you need

LM35

How to run the example

Go to py-arduino directory and activate the virtualenv
~/$  cd py-arduino
~/py-arduino$  . virtualenv/bin/activate
Launch the example:
~/py-arduino$ python -m examples.read_lm35_basic /dev/ttyACM0

LM35

Code:

from py_arduino.main_utils import BaseMain

PIN = 0

class Main(BaseMain):

<span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">arduino</span><span class="p">):</span>
    <span class="n">value</span> <span class="o">=</span> <span class="n">arduino</span><span class="o">.</span><span class="n">analogRead</span><span class="p">(</span><span class="n">PIN</span><span class="p">)</span>
    <span class="n">temp</span> <span class="o">=</span> <span class="p">(</span><span class="mf">5.0</span> <span class="o">*</span> <span class="n">value</span> <span class="o">*</span> <span class="mf">100.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">1024.0</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Temperature: {0:3.2f} C"</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span>

if name == 'main': Main().start()