0

Newbie question on PWM
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Newbie question on PWM

by Bansuri on Wed Feb 12, 2020 9:03 am

Hi,
I've just gotten started with the Trinket M0 and having some starting trouble. The LED (+ to pin and -ve to GND) is connected to Pin 0 on the board and have tried changing the nomenclature to A2, 11, 0, and uploading the code. The code uploads but no fade in either of these cases. Naming the pin D0 doesn't seem to be acceptable.

Can the experts please tell me where I'm going wrong!

Code: Select all | TOGGLE FULL SIZE
int led = A2;           // the LED is connected to the pin 0 on the board
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
  analogWrite(led, brightness);

  brightness = brightness + fadeAmount;

  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  delay(100);
}

Bansuri
 
Posts: 3
Joined: Wed Feb 12, 2020 8:48 am

Re: Newbie question on PWM

by dastels on Wed Feb 12, 2020 12:40 pm

0 is what you want to use for the LED pin variable. Overall the code looks fine.

Is the LED always off?

You need a 50-100 ohm resistor between the pin and the LED's anode (see https://learn.adafruit.com/all-about-leds) when connecting it to as 3.3v signal. Without that it's quite possible that the LED is burnt out.

Dave

dastels
 
Posts: 1643
Joined: Tue Oct 20, 2015 3:22 pm

Re: Newbie question on PWM

by Bansuri on Thu Feb 13, 2020 5:02 am

Thank you ..I put a multimeter and checked that the voltage was varying and replaced the LED as per your suggestion, along with a resistor and its working now.

The larger idea was to get a DC motor working with the Trinket M0 by using a regular NPN transistor such as BC337.

I'm using the same schematic as shown here, https://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors

the exception being that I'm using a Trinket M0 instead of the Uno. The motor, I'm trying to drive is a tiny 7mm one, like here:https://www.amazon.com/uxcell-Coreless-Helicopter-Propellers-Quadcopter/dp/B06ZY29WSR/ref=sr_1_36?keywords=7mm+motor&qid=1581583630&sr=8-36

Does the transistor Vbe need to be above what is fed by the Trinket for it to work ?

Thanks!

Bansuri
 
Posts: 3
Joined: Wed Feb 12, 2020 8:48 am

Re: Newbie question on PWM

by dastels on Thu Feb 13, 2020 10:38 am

It'll be fine. To drive the transistor into saturation (make it work like a switch rather than an amplifier) Vbe just needs to be 0.6v. See my guide on transistos, particularly the example and faq pages. https://learn.adafruit.com/transistors-101 and my article in issue 13 of Hackspace https://hackspace.raspberrypi.org/issues/13.

Dave

dastels
 
Posts: 1643
Joined: Tue Oct 20, 2015 3:22 pm

Re: Newbie question on PWM

by Bansuri on Fri Feb 14, 2020 1:11 am

Thanks again Dave, for taking the time out and showing me the basics. It helped and I was able to get the PWM working with my transistor and Trinket combo!

Regards!

Bansuri
 
Posts: 3
Joined: Wed Feb 12, 2020 8:48 am

Re: Newbie question on PWM

by dastels on Fri Feb 14, 2020 9:30 am

Excellent!

Dave

dastels
 
Posts: 1643
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.