analogWrite only works on PWM slots (Teensy 2.0)

Status
Not open for further replies.

wsme

Member
Hey all,

Very new to circuit design. Just learned analogRead() for analog input. Wanted to make a toggle switch, so I used the following code:
Code:
// Toggle button

#define OUTPIN 12      //<- THIS MACRO
void setup() {
    Serial.begin(38400);
    pinMode(OUTPIN, OUTPUT);
}

int input;
int state = 0;

void loop() {
    input = analogRead(10);
    if (input > 400 && !(state)) {
        analogWrite(OUTPIN, 1);
//        Serial.print("Success\n");
        state = 1;
    } else if (analogRead(10) > 400 && state) {
//        Serial.print("Success2\n");
        analogWrite(OUTPIN, 0);
        state = 0;
    }
    delay(100);
}

This works when OUTPIN is one of the PWM ports listed on the pinout reference page (12, 14, 15).
Below is the circuit used for a working design from 2 angles:
http://https://imgur.com/a/rYl72Ug

Why does the circuit only work when power is given to the pwm pins?
 
IIRC - Analog usage fails with pinmode use setting it to digital mode.

Change the setup to:
Code:
void setup() {
    Serial.begin(38400);
    [B]while (!Serial && millis() < 2000 );[/B] // Add this to allow 2 seconds for USB connect so first prints are not lost.
    //[COLOR="#FF0000"]pinMode(OUTPIN, OUTPUT);[/COLOR]
}

Also if you want digital ON or OFF values - use digitalWrite() or digitalWriteFast() [with a constant pin # is faster]


Writing an analog value of '1' will be a minimal value? With resolution at 12 bits - it would be 1/4096 of 3.3V. For Full value use analogWrite(OUTPIN, 4096); with 'analogWriteResolution(12);' in setup().
 
AnalogWrite is pulse width modulation.

....and 4095 is maximum value and the correct divisor for scaling 12 bit values.
 
Opps - This is Teensy 2 so indeed this note may not apply - so it would be 8 bits write 255 for Max value:
PWM Resolution (Teensy LC, 3.0 - 3.6)
Normally analogWrite(pin, value) is used with the value between 0 to 255, which corresponds to 8 bit resolution. Writing 256 forces the pin always high. Teensy LC &x:w, 3.x support an analogWriteResolution(bits) function, to reconfigure analogWrite.

void setup() {
analogWriteResolution(12); // analogWrite value 0 to 4095, or 4096 for high
}
 
Last edited:
So writing 2^N will set HIGH? Didn't know that.

Still not the correct scaling divisor (although I had to think it through at N=2 to be sure).
 
...Still not the correct scaling divisor (although I had to think it through at N=2 to be sure).
Or is it? I guess with a the convention that 2^N = HIGH it could be 2^N-1 is off 1/2^N of the time... but I wouldn't think so.
 
Teensy 2 CORE sets the value given it seems, Teensy3_ARM cores special case the Max value and set digital HIGH.

...\hardware\teensy\avr\cores\teensy3\pins_teensy.c
Code:
else if (val >= max) {
		digitalWrite(pin, HIGH);
		pinMode(pin, OUTPUT);	// TODO: implement OUTPUT_HIGH
		return;
	}

Paul: Should the pinMode() be before the digitalWrite()?
 
IIRC - Analog usage fails with pinmode use setting it to digital mode.

Change the setup to:
Code:
void setup() {
    Serial.begin(38400);
    [B]while (!Serial && millis() < 2000 );[/B] // Add this to allow 2 seconds for USB connect so first prints are not lost.
    //[COLOR="#FF0000"]pinMode(OUTPIN, OUTPUT);[/COLOR]
}

Also if you want digital ON or OFF values - use digitalWrite() or digitalWriteFast() [with a constant pin # is faster]


Writing an analog value of '1' will be a minimal value? With resolution at 12 bits - it would be 1/4096 of 3.3V. For Full value use analogWrite(OUTPIN, 4096); with 'analogWriteResolution(12);' in setup().

Oh wow, that was really insightful. I had not realized the difference between digitalWrite() and analogWrite().
Do most commands with "analog" in the function name also have a digital version? (ie digitalRead())

(thanks a ton)
 
Status
Not open for further replies.
Back
Top