cylon-gpio icon indicating copy to clipboard operation
cylon-gpio copied to clipboard

analogWrite doesn't work

Open didasy opened this issue 7 years ago • 5 comments

I'm currently trying to do analogWrite(255) to pin 5 of an Arduino Uno, but it doesn't give me voltage reading at all. (it toggle the LED though)

I've tried it using regular arduino code directly on the board and at 255 it gives me around 4.8v.

This is my code:

"use strict";

const config = require(__dirname + "/config.json");
config.work = require(__dirname + "/lib/work");

(require("cylon")).robot(config).start();

this is the config part

{
    "name": "Testbot",
    "connections": {
        "arduino": { 
            "adaptor": "firmata", 
            "port": "/dev/ttyUSB0" 
        }
    },
    "devices": {
        "onboardLED": { 
            "driver": "led", 
            "pin": 13 
        },
        "writer": {
            "driver": "direct-pin",
            "pin": 5
        }
    }
}

and this is the work code

dev.onboardLED.toggle();
dev.writer.analogWrite(255);

didasy avatar Jan 02 '17 11:01 didasy

When using Firmata pins A4 and A5 are used by the i2c interface. I suggest using pins A0 thru A3.

Hope that helps!

deadprogram avatar Jan 02 '17 12:01 deadprogram

I am using digital pin 5 (as it says it has PWM support), not analog A5. Or are you saying they are connected?

didasy avatar Jan 02 '17 17:01 didasy

OK. I think you should use pwmWrite() since it will do the mapping you expect (digital pins).

analogWrite() does the same thing as analogRead() which takes an analog pin number as a param.

deadprogram avatar Jan 02 '17 18:01 deadprogram

Hold on, I thought analogWrite() accept PWM value (0-255) as param? What does it do exactly?

And I think I will try using pwmWrite() instead, thanks for the pointer.

didasy avatar Jan 03 '17 04:01 didasy

The difference is that analogWrite() when you pass "5" as a param, it means pin A5. With pwmWrite() the param "5" means pin D5.

deadprogram avatar Jan 03 '17 10:01 deadprogram