Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read an analog value from a pwm port (arduino) with pyfirmata2 on Python #99

Open
wants to merge 154 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
154 commits
Select commit Hold shift + click to select a range
5a51489
Added an event handler which is called at the sampling rate
berndporr Sep 7, 2018
996f644
Updated README with the newest info
berndporr Sep 7, 2018
e295c08
Added a thread to the class board which samples then at a
berndporr Sep 8, 2018
f37f064
Removed actually util from it.
berndporr Sep 8, 2018
d6acd29
Fixed error messages from the automated build system on github
berndporr Sep 8, 2018
d3764cc
Updated the doc
berndporr Sep 10, 2018
89505ff
I have been testing it under Python 3.6 which works fine.
berndporr Sep 10, 2018
0a20bf8
Added the new commands to the changes
berndporr Sep 10, 2018
dfa4905
Added an object oriented data reader which can be used
berndporr Sep 10, 2018
bf01e27
Fixed build errors reported from Travis
berndporr Sep 10, 2018
a8661c1
More fixes suggested by Travis
berndporr Sep 10, 2018
4945115
Shuts down the iterator thread when not called explicitely.
berndporr Sep 10, 2018
39b0940
Fixed crash when the thread is not running
berndporr Sep 10, 2018
0d02298
Added two examples
berndporr Sep 11, 2018
aaf19d1
Fixed issues reported by Travis
berndporr Sep 11, 2018
63e96c4
removed two whitespaces
berndporr Sep 11, 2018
26cc13b
Added the samplerThread to the mockup
berndporr Sep 11, 2018
175f124
Overseen that there is a also an Iterator mockup!
berndporr Sep 11, 2018
862de01
Now object oriented
berndporr Sep 12, 2018
a955588
Added myself as author to it and python 3.5 works as well
berndporr Sep 12, 2018
d6ccd62
Removed a whitespace
berndporr Sep 12, 2018
fb4015c
Fixed another Travis issue
berndporr Sep 12, 2018
db1c84b
Added max sampling rate which is 100Hz
berndporr Sep 12, 2018
f48afdb
Changed the name to pyfirmata2 for the master branch
berndporr Sep 14, 2018
3eaf9d1
Fixed two references to pyfirmata
berndporr Sep 14, 2018
b646aac
Updated the author list
berndporr Sep 14, 2018
0c586dd
Made it executable
berndporr Sep 14, 2018
49ea125
Updated the readme
berndporr Sep 14, 2018
19b4d1c
Setting the inverval to default interval of 19ms
berndporr Sep 14, 2018
c41b817
Updated the version number and README
berndporr Sep 15, 2018
cf6a0a4
Fixed typos and added a text to the examples.
berndporr Sep 15, 2018
f123f15
Added more meat to the description
berndporr Sep 16, 2018
35c84ee
Tiny formatting issues
berndporr Sep 16, 2018
1149766
Small fixes
berndporr Sep 16, 2018
81f1bfb
Some advanced formatting instructions made pypi ignore them all.
berndporr Sep 16, 2018
610fc0a
Added filtering to the realtime scope
berndporr Sep 16, 2018
699e10c
Added more explanations
berndporr Sep 16, 2018
f571865
Forgot the filtering
berndporr Sep 16, 2018
f7afbfc
typo
berndporr Sep 16, 2018
7f0836d
Added a screenshot
berndporr Sep 16, 2018
086bb49
New version and some small fixes in the README
berndporr Sep 16, 2018
4a02161
Added a README to the examples dir
berndporr Sep 16, 2018
37af1fd
Fixed a pesky spelling mishtake!
berndporr Sep 21, 2018
d114935
Added port to mac
berndporr Nov 19, 2018
2dc22e0
Split it up for Windows
berndporr Nov 21, 2018
d74cb7a
Added info about mac
berndporr Nov 21, 2018
de6dffc
Created new release and indicated that lfilter might not work.
berndporr Nov 25, 2018
3649873
Added another MAC device.
berndporr Nov 25, 2018
403a13b
Added another serial device
berndporr Nov 25, 2018
8781975
Updated the readme
berndporr Nov 26, 2018
336dfc1
Fixed a colon
berndporr Nov 26, 2018
f5fae6b
A few more fixes.
berndporr Nov 26, 2018
fcf635c
New version no
berndporr Nov 26, 2018
6202c75
Removed some leftovers
berndporr Nov 26, 2018
cdb5430
Made clearer that for most operations the pin class is needed.
berndporr Nov 26, 2018
3b5c403
GRammar!
berndporr Nov 26, 2018
67755d5
Removed the lfilter command.
berndporr Nov 26, 2018
b989894
updated readme
berndporr Nov 26, 2018
0ff1bfd
Added autodetect
berndporr Nov 26, 2018
c060690
Condition in case no device is found.
berndporr Nov 26, 2018
59b5325
Made the error handling watertight
berndporr Nov 26, 2018
c88ca79
Added autodetect for the serial port. Tested only under Linux so far.
berndporr Nov 26, 2018
0083033
More info in the examples
berndporr Nov 26, 2018
8a8df46
Made it more plug and play for Linux and hopefully windows.
berndporr Nov 26, 2018
b823663
Improved autodetect for Windows. Assuming that
berndporr Nov 26, 2018
2434ee5
Added also some vendor ID testing for MAC.
berndporr Nov 27, 2018
c583088
New version
berndporr Nov 27, 2018
e323306
Changed it all to autodetect
berndporr Nov 27, 2018
b2906af
Updated the autoconfig
berndporr Nov 27, 2018
7ec8296
Fixed a couple of spelling mishtakes.
berndporr Nov 27, 2018
8ca5f72
Removed two files which are no longer needed
berndporr Nov 29, 2018
e0cc0c4
Throws an exception if sampling rate is higher than 100Hz
berndporr Nov 29, 2018
d620256
Add info about 2nd port.
berndporr Nov 29, 2018
5921afc
10ms is the hard limit
berndporr Nov 29, 2018
6db2305
Tense!
berndporr Nov 29, 2018
615039a
Two channel version
berndporr Nov 30, 2018
0d96e7b
Fixed a spelling mistake
berndporr Dec 1, 2018
11cd58a
Fixed a couple of smaller issues in the text
berndporr Dec 1, 2018
45c3b0b
Added exit() so that the serial port is definitely closed.
berndporr Dec 2, 2018
8b2474d
Changed the names of the scopes for consitency
berndporr Dec 2, 2018
1c88e8c
Added troubleshooting section.
berndporr Dec 12, 2018
9f487b0
Added more troubleshooting
berndporr Dec 13, 2018
fb3ccc8
exit()
berndporr Dec 13, 2018
d3831cb
Added link to IDE
berndporr Dec 14, 2018
f3b3487
New major version to reflect that it's a departure form pyfirmata
berndporr Dec 18, 2018
6450dfa
Link to the YouTube channel
berndporr Dec 26, 2018
9c3c02d
New version
berndporr Dec 26, 2018
0e435af
Made sure that the flashing light demo is not a precise realtime
berndporr Nov 30, 2019
bc0d342
Added info where to put the filter code
berndporr Dec 4, 2019
5909096
Made all python files directly executable under Linux.
berndporr Dec 4, 2019
d10f8d4
Switched back to autodetect
berndporr Nov 11, 2020
5c185dd
Made the V2 feature clearer.
berndporr Nov 13, 2020
18b404a
Made the evil pass_time function private.
berndporr Nov 26, 2020
67990ef
fs been changed but not comment
berndporr Nov 26, 2020
c1872ce
Added copyright terms to it.
berndporr Dec 3, 2020
0795f74
Added copyright notice
berndporr Dec 3, 2020
829331c
Bugfix from @gastonfeng that d.vid is not set.
berndporr Mar 6, 2021
84d9b2c
New version number and tested under Windows
berndporr Mar 11, 2021
d4f9e53
Created readme for github
berndporr Mar 12, 2021
fc690af
Removed a double ::
berndporr Mar 12, 2021
f98ba95
The firmata firmware seems to have changed the minimum sampling
berndporr Aug 23, 2021
d9ffb8c
Applied a few patches from the original pyFirmata
berndporr Aug 24, 2021
177dbcd
Tested the 1kHz sampling rate and seems to be OK
berndporr Aug 25, 2021
535e27f
Fixed description
berndporr Aug 26, 2021
b5c0cbd
Added support for INPUT_PULLUP mode
benjie-git Oct 1, 2021
9d4641a
Merge pull request #6 from benjie-git/master
berndporr Oct 2, 2021
93645f5
bumped up version as we have now also pullup support
berndporr Oct 3, 2021
f248ead
Added the pullup to the text
berndporr Oct 23, 2021
e3be3fb
Added also pullup in the python readme
berndporr Oct 23, 2021
4f6519a
Made it in bold face that it's about callbacks and not loops.
berndporr Nov 7, 2021
995ed39
Same text now also for pypi
berndporr Nov 7, 2021
8d023cc
Added callback for digital IO
berndporr Nov 14, 2021
863af26
Blinking now also uses a callback
berndporr Nov 14, 2021
4bd3f0b
Got rid of a hack for the two channel plot
berndporr Nov 14, 2021
fa56ae9
Synced the rst and md.
berndporr Nov 14, 2021
77da992
Changed it to a class
berndporr Nov 15, 2021
3cc9734
fixed the wrong fs in the comment
berndporr Nov 17, 2021
d243107
Removed specifics from the pypi readme.
berndporr Nov 18, 2021
b40d3f2
Found more typos here about firmata vs firmata2
berndporr Nov 18, 2021
dedc637
Added pwm and servo demo
berndporr Nov 18, 2021
5dafffb
fixed a few small things
berndporr Nov 18, 2021
6894c23
Added more info about PWM, servo and callback handlers
berndporr Nov 19, 2021
bcdc30d
Added more documentation
berndporr Nov 19, 2021
04c3652
Added a warning if somebody uses the read command on input pins
berndporr Nov 19, 2021
be9918f
New version number
berndporr Nov 19, 2021
395244e
Added class ref to the readme
berndporr Nov 19, 2021
8d251bc
made the executable
berndporr Nov 19, 2021
2dd3c2d
Put a link to the orig pyFirmata
berndporr Dec 5, 2021
d5c4ce5
broken link
berndporr Dec 5, 2021
48ba165
Not just spyder can screw it up.
berndporr Jan 2, 2022
7c7a621
Broken
berndporr Nov 10, 2022
31e2141
Updated the demos so that they work with the new version of pyqtplot
berndporr Nov 10, 2022
a2d444e
Added more info how to upload firmata.
berndporr Nov 10, 2022
2996714
Title was wrong.
berndporr Nov 10, 2022
c1ac5c0
Removed unneccesary code and fixed a few comments.
berndporr Nov 10, 2022
6d43dbe
Done it properly to show that it's now a widget.
berndporr Nov 11, 2022
976a521
Added a label.
berndporr Nov 11, 2022
d11f5c1
Added debug message for the port
berndporr Dec 20, 2022
88f9a93
inspect.getargspec is deprecated
berndporr Nov 2, 2023
d211914
Windows: scans only for USB serial devices
berndporr Nov 3, 2023
017eb72
Added a scanner for COM ports
berndporr Nov 3, 2023
b5e8109
Added info about scan.py
berndporr Nov 3, 2023
5c2d370
Incremented version
berndporr Nov 3, 2023
40c01a3
Added info about inputs and outputs
berndporr Nov 10, 2023
dacbd83
Spelling mistake
berndporr Nov 10, 2023
2e12ee6
Added that it's an integer number
berndporr Dec 21, 2023
a51be9a
initialize pin.mode when using digital output
Feb 20, 2024
d0a41a3
Added disable_reporting() on exit
berndporr Feb 23, 2024
63ac54d
Merge branch 'digital-output-pin-initialization' of https://github.co…
berndporr Feb 23, 2024
48f97e2
Improved code to have a proper fallback to input pin mode.
berndporr Feb 23, 2024
952fdd3
Merge branch 'Rotule666-digital-output-pin-initialization'
berndporr Feb 23, 2024
a43ee10
Polling of analogue or digital pins now causes an exception
berndporr Apr 18, 2024
1f10d93
Fixes crash if no com port has been found under Windows
berndporr Jun 5, 2024
967da5a
This should resolve issue https://github.com/berndporr/pyFirmata2/iss…
berndporr Jun 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .hgignore

This file was deleted.

8 changes: 0 additions & 8 deletions .hgtags

This file was deleted.

22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

30 changes: 0 additions & 30 deletions CHANGES.rst

This file was deleted.

1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Copyright (c) 2010, Tino de Bruijn
Copyright (c) 2018-2020, Bernd Porr <[email protected]>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
250 changes: 250 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
# pyFirmata2

![alt tag](screenshot.png)

PyFirmata2 turns your Arduino into an analogue to digital data acquistion
card controlled by Python.
Just upload the default firmata sketch into your Arduino and you are all set.

## Inputs
- Analogue, measuring volt. Up to 1kHz precise sampling.
- Digital, reacting to digital state changes.

## Outputs
- Digital out
- PWM
- Servo control

No loops and "sleep" commands: pyFirmata2 is an updated version of
pyFirmata which *replaces loops with callbacks*. Instead of unreliable
and disruptive "sleep" commands in a loop the Python application
registers callbacks with pyfirmata2 which are then called every time
new data has arrived. This means for the analogue channels the
callbacks are called at the specified sampling rate while the digital
ports call the callback functions after a state change at the port
(from 0 to 1 or 1 to 0).

This API has been used in the Digital Signal Processing (DSP) class to
practise realtime filtering of analogue sensor
data. Examples can be viewed on the YouTube channel of the
class: https://www.youtube.com/user/DSPcourse


## Installation



### Upload firmata


Install the Arduino IDE on your computer: https://www.arduino.cc/en/Main/Software

- Start the Arduino IDE
- Select the serial port under "Tools"
- Select your Arduino board under "Tools"
- Upload the standard firmata sketch to your Arduino with:
```
File -> Examples -> Firmata -> Standard Firmata
```


### Install pyfirmata2


The preferred way to install is with `pip` / `pip3`. Under Linux:
```
pip3 install pyfirmata2 [--user] [--upgrade]
```

and under Windows/Mac type:
```
pip install pyfirmata2 [--user] [--upgrade]
```

You can also install from source with:
```
git clone https://github.com/berndporr/pyFirmata2
cd pyFirmata2
```

Under Linux type:
```
python3 setup.py install
```

Under Windows / Mac:
```
python setup.py install
```

## Usage


### Initialisation

Create an instance of the `Arduino` class:
```
PORT = pyfirmata2.Arduino.AUTODETECT
board = pyfirmata2.Arduino(PORT)
```
which automatically detects the serial port of the Arduino.

If this fails you can also specify the serial port manually, for example:
```
board = pyfirmata2.Arduino('COM4')
```
Under Linux this is usually `/dev/ttyACM0`. Under Windows this is a
COM port, for example `COM4`. On a MAC it's `/dev/ttys000`, `/dev/cu.usbmodem14101` or
check for the latest addition: `ls -l -t /dev/*`.


### Starting sampling at a given sampling interval

In order to sample analogue data you need to specify a sampling
interval in ms which then applies to all channels. The smallest
interval is 1ms:
```
board.samplingOn(samplinginterval in ms)
```
Note that the sampling interval is an *integer* number.
Calling `samplingOn()` without its argument sets the sampling interval
to 19ms.


### Enabling and reading from analogue or digital input pins

To receive data register a callback
handler and then enable it:
```
board.analog[0].register_callback(myCallback)
board.analog[0].enable_reporting()
```
where `myCallback(data)` is then called every time after data has been received
and is timed by the arduino itself. For analogue inputs that's at
the given sampling rate and for digital ones at state changes from 0 to 1 or
1 to 0.

### Writing to a digital port

Digital ports can be written to at any time:
```
board.digital[13].write(True)
```
For any other functionality (PWM or servo) use the pin class below.


### The pin class

The command `get_pin` requests the class of a pin
by specifying a string, composed of
'a' or 'd' (depending on if you need an analog or digital pin), the pin
number, and the mode:
- 'i' for input (digital or analogue)
- 'u' for input with pullup (digital)
- 'o' for output (digital)
- 'p' for pwm (digital)
- 's' for servo (digital)
All seperated by `:`, for example:
```
analog_in_0 = board.get_pin('a:0:i')
analog_in_0.register_callback(myCallback)
analog_in_0.enable_reporting()

digital_out_3 = board.get_pin('d:3:o')
digital_out_3.write(True)
```
Values for analogue ports and PWM are 0..1,
for servo between 0 and 180 (degrees) and for digital ports
`True` & `False`.

```
class Pin(builtins.object)
| Pin(board, pin_number, type=2, port=None)
|
| A Pin representation
|
| Methods defined here:
|
| __init__(self, board, pin_number, type=2, port=None)
| Initialize self. See help(type(self)) for accurate signature.
|
| __str__(self)
| Return str(self).
|
| disable_reporting(self)
| Disable the reporting of an input pin.
|
| enable_reporting(self)
| Set an input pin to report values.
|
| read(self)
| Returns the value of an output pin.
|
| register_callback(self, _callback)
| Register a callback to read from an analogue or digital port
|
| :arg value: callback with one argument which receives the data:
| boolean if the pin is digital, or
| float from 0 to 1 if the pin is an analgoue input
|
| unregiser_callback(self)
| Unregisters the callback which receives data from a pin
|
| write(self, value)
| Output a voltage from the pin
|
| :arg value: Uses value as a boolean if the pin is in output mode, or
| expects a float from 0 to 1 if the pin is in PWM mode. If the pin
| is in SERVO the value should be in degrees.
|

```

### Closing the board

To close the serial port to the Arduino use the exit command:
```
board.exit()
```

## Example code

The directory https://github.com/berndporr/pyFirmata2/tree/master/examples
contains two realtime Oscilloscopes with precise sampling rate,
a digital port reader, the ubiquitous flashing LED program, pwm, servo control
and
a program which prints data using the callback handler.


## Troubleshooting

### Spyder / pyCharm / IDEs

Start your program from the console / terminal and never within an IDE. Here is
an example for Windows:
```
(base) D:\>
(base) D:\>cd pyFirmata2\examples
(base) D:\pyFirmata2\examples>python realtime_two_channel_scope.py
```
The problem with IDEs is that they won't let your Python program terminate properly
which leaves the serial port in an undefined state. If you then re-run your program
it won't be able to talk to your Arduino. In the worst case you need to reboot your
computer. Bottomline: use your IDE for editing, run the program from the console / terminal.


### After an update still the old version is being used

If you use the `--user` option to install / update packages Python might keep older versions.

Solution: Do a `pip uninstall pyfirmata2` multiple times until no version is left
on your computer. Then install it again as described above.




### Credits

The [original pyFirmata](https://github.com/tino/pyFirmata)
was written by Tino de Bruijn and is recommended if you'd rather
prefer loops and sleep()-commands.
Loading