2

I am trying to use Arduino analog pins (A0-A5) as digital pins (D14-D19) with pyfirmata. I'm doing a simple blink test with D14 (or A0) to test this:

#!/usr/bin/python

# Blink test with analog as digital pins

# Import required libraries
from pyfirmata import Arduino, util
from pyfirmata import INPUT, OUTPUT, PWM  
from time import sleep

# Associate port and board with pyFirmata
port = '/dev/cu.usbmodem1451'
board = Arduino(port)

# Pause to sync
sleep(1)

led = board.get_pin('d:14:o')
time = 1

while True:
    led.write(1)
    print ("On")
    sleep(time)
    led.write(0)
    print ("Off")
    sleep(time)

To enable analog pins to act as digital pins, I've gone into pyfirmata's boards.py file (as show in the github repo at https://github.com/tino/pyFirmata/blob/master/pyfirmata/boards.py) and changed line #3 from

digital': tuple(x for x in range(14)),

to

digital': tuple(x for x in range(20)),

I don't get an error when I run my python script, but the LED doesn't blink (don't worry, I'm positive the LED was in the right way).

Does anyone know how I can effectively instantiate digital pin 14 as an output pin with pyfirmata (as in the line led = board.get_pin('d:14:o'))

By the way (not that I think this affects anything) but I'm running Mac OSX with Yosemite. Thank you!

Dot Silverman
  • 29
  • 1
  • 2

1 Answers1

0

The standard firmata that we use in Arduino, makes all analog pins input by default. So instead of changing anything in boards.py file, go to standard firmata file in arduino ide and find this ...

switch (mode) {
    case PIN_MODE_ANALOG:
      if (IS_PIN_ANALOG(pin)) {
        if (IS_PIN_DIGITAL(pin)8) {
         pinMode(PIN_TO_DIGITAL(pin), INPUT);

Now comment the pinMode line so that the firmata does not make any analog pin input Now go to void setup() function and explicitly define nature of analog pins as shown below.

Firmata.begin(57600);
  while (!Serial) {
  }
  pinMode(14,OUTPUT);
  pinMode(15,INPUT);
  pinMode(16,INPUT);
  pinMode(17,INPUT);
  pinMode(18,INPUT);
  pinMode(19,INPUT);
  digitalWrite(14,LOW);