View Full Version : Need help with analogWrite and digitalWrite on same pin

01-18-2013, 06:25 PM

I have ported a working sketch from Arduino to Teensy 3.0. All of it works beautifully except when I try to get a PWM output from a pin. It seems I am behind the times because this simple code does nothing on the pin when I try to analog write to it. I am running Beta11 on Ubuntu 12.04. My code is pretty complicated, but if I can get this simple sketch working, I think I can get this put into the code... I will need a digitalWrite and a PWM on the pin for the sketch to function correctly.

adapted from the statechange example. If I do this, the pin will flop from high to low:

#include <Bounce.h>
#define BUTTON 0
#define LED 13
#define pwm_output 8

int ledValue = LOW;
int pwmValue = 100;
// This example changes the state of the LED everytime the button is pushed
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button

Bounce bouncer = Bounce( BUTTON, 15 );

void setup() {
pinMode(pwm_output, OUTPUT);

void loop() {

if ( bouncer.update() ) {
if ( bouncer.read() == HIGH) {
if ( ledValue == LOW ) {
ledValue = HIGH;
pwmValue = 100;
} else {
ledValue = LOW;
pwmValue = 5;
digitalWrite(pwm_output, ledValue);

if I change the last line to:

analogWrite(pwm_output, pwmValue);

The pin stays at LOW and nothing happens.



01-18-2013, 07:08 PM

Sorry for the sideshow. I was using a pin which is not PWM capable...

Now I can get it to work, but have to redefine the pin when I use it as digital or PWM. I get it now.