[SOLVED] Pi Zero analogWrite()

After doing some testing of particle-agent, I can’t seem to get pwm to work on the raspberry pi zero. I’ve tested ever pin listed as PWM in the docs, however if analogWrite is called to that pin there is no perceptible output (Don’t own an oscilloscope to look closer). Code is simply

// Define the pins we're going to call pinMode on
int led = D6; // This one is the built-in tiny one to the right of the USB jack

// This routine runs only once upon reset
void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop() {
//   /*
  for(int i = 0; i<256; i++) {
      analogWrite(led,i);
      Serial.println(i);
      delay(30);
  }
  digitalWrite(led,HIGH);
  for(int i = 255; i>=0; i--) {
      analogWrite(led,i);
      Serial.println(i);
      delay(30);
  }
  digitalWrite(led,LOW);
}

I have serial in there to make sure it’s running through particle-agent serial. Even when the code reaches the digitalWrite() the led connected to the pin I’m testing doesn’t come on. When the analogWrite is removed the digitalWrites work as expected, turning the LED on and off.

Also PiZero is set up as a USB ethernet gadget (see here) for ease of use at my desk.

Any help?

Thanks

Currently on Raspberry Pi, every time you want to do analogWrite(), you must first do pinMode().

Thus, your loop would look like:

void loop() {
  for(int i = 0; i<256; i++) {
      pinMode(led, OUTPUT);
      analogWrite(led,i);
      Serial.println(i);
      delay(30);
  }
  digitalWrite(led,HIGH);
  for(int i = 255; i>=0; i--) {
      pinMode(led, OUTPUT);
      analogWrite(led,i);
      Serial.println(i);
      delay(30);
  }
  digitalWrite(led,LOW);
}

That works thanks!

2 Likes