I wanted to ask that how to run analog things like pot's with pyfirmata. So I just started with the firmata protocol and I want to learn how to control potentiometers. For example I am just making a project that controls servo's with potentiometers just to understand the concept. Can you also tell me that the analogWrite() function we use in the Arduino IDE to control the speed of motors sometimes. It doesn't work there for me I tried, so what will be the code for that in python? Here is the sample code that is probably wrong:
import pyfirmata
from pyfirmata import SERVO
Board = pyfirmata.Arduino('COM4')
servo_pin = Board.get_pin('d:3:o')
Board.digital[3].mode = SERVO
Pot = Board.get_pin('a:1:i')
if Pot == Pot + 1 :
servo_pin.write(Pot)
If you can can you recommend some good tutorials on Pyfirmata for syntax and all. Thanks...
oh... yup realized.
Ok this is I think a better version but I am getting this error.:
import pyfirmata
import time
from pyfirmata import SERVO
Board = pyfirmata.Arduino('COM3')
pot = Board.get_pin('a:1:i')
S1 = Board.get_pin('d:3:o')
Board.digital[3].mode = SERVO
it = pyfirmata.util.Iterator(Board)
it.start()
while True:
AnalogVal = pot.read()
print(AnalogVal)
S1.write(AnalogVal)
time.sleep(0.1)
Error:
S1.write(AnalogVal)
File "D:\Users\swara\AppData\Local\Programs\Python\Python310\lib\site-packages\pyfirmata\pyfirmata.py", line 547, in write
value = int(value)
ValueError: invalid literal for int() with base 10: 'None'
He is saying that he had to upload the firmata skech before executing the program or something like that but you have to upload the skech either way. So I don't understand. So if you can you explain it to me why it is happening or give me a solution that will be appreciated. Please this is just to tell you what I am asking for and please don't take this the wrong way. Thank you...
Hello again I tried a lot of things but 1 worked the error message is gone but I am getting a new error message. Analog 1 is not an Input pin therefore cannot report what should I do?
Firmata is software you load into an Arduino. Then you use another program on your PC to send messages over the serial port to the Arduino running Firmata, and it sends back answers to your PC serial port. Unless you are a developer trying to change Firmata you don't program your Arduino with anything else.
You need to be programming in some language like Python, Processing or C to name a few that can send strings to Firmata.
Hello, thanks for you response, now the original error is gone but I am getting another error in which it is saying that the analog 1 pin is not set as input thus cannot report. How do I solve that?
Thanks...
import pyfirmata
import time
from pyfirmata import SERVO
from pyfirmata import ANALOG
Board = pyfirmata.Arduino('COM3')
it = pyfirmata.util.Iterator(Board)
it.start()
pot = Board.get_pin('a:1:i')
S1 = Board.get_pin('d:3:o')
Board.digital[3].mode = SERVO
Board.analog[1].mode = ANALOG
Board.analog[1].enable_reporting()
while True:
AnalogVal = pot.read()
print(AnalogVal)
S1.write(AnalogVal)
time.sleep(0.1)
I tried removing the line Board.analog[1].enable_reporting()
but then it gives the error:
value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
And if I keep the line I have this error:
raise IOError("{0} is not an input and can therefore not report".format(self))
OSError: Analog pin 1 is not an input and can therefore not report
Where is this code run? Is it on your PC?
Have you a link to the pyfirmata module.
I have not used this before but try this
import pyfirmata
import time
from pyfirmata import SERVO
from pyfirmata import ANALOG
Board = pyfirmata.Arduino('COM3')
it = pyfirmata.util.Iterator(Board)
it.start()
pot = Board.get_pin('a:1:i')
S1 = Board.get_pin('d:3:o')
Board.analog[0].enable_reporting()
Board.digital[3].mode = SERVO
Board.analog[0].mode = ANALOG
while True:
AnalogVal = pot.read()
print(AnalogVal)
S1.write(AnalogVal)
time.sleep(0.1)
pyfirmata Is not highly praised when it’s being discussed. Instead of loosing time there Why don’t you try to program your arduino and your Python code yourself so that you can be in charge of the overall solution? (And learn a few things along the way)
Hello I actually tried fixing it already many times but with no luck but still the error remains.
That is why I am Asking the forum. This is actually a school project that I have to submit in the next 2 days so I really need the replies to be quick.
Thanks...
what do you see when you execute the following python code?
from pyfirmata import Arduino, util
board = Arduino('COM3')
it = util.Iterator(board)
it.start()
board.analog[0].enable_reporting()
board.analog[0].read()
EDIT:
just got to try this on my UNO and my Mac
I launched python3 and just typed in the code above (suited for my Serial port where my UNO sits and on which I had previously installed the standardFirmata example code) and asked for the value of A0 a few times
$python3
Python 3.7.3 (default, Mar 6 2020, 22:34:30)
[Clang 11.0.3 (clang-1103.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyfirmata import Arduino, util
>>> board = Arduino('/dev/cu.usbmodem21401')
>>> it = util.Iterator(board)
>>> it.start()
>>> board.analog[0].enable_reporting()
>>> board.analog[0].read()
0.4604 // <=== Note: here I left A0 floating
>>> board.analog[0].read()
0.0 // <=== Note: here I connected A0 to GND
>>> board.analog[0].read()
1.0 // <=== Note: here I connected A0 to 5V
>>> board.analog[0].read()
0.6403 // <=== Note: here I connected A0 to 3.3V
>>>
all seems to work fine and the value reported makes sense, it's a floating point value between 0 and 1 that maps to GND - VCC of your board.