Simple Pulsing LED for a Noob

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Virtuous70
 
Posts: 23
Joined: Sat Nov 04, 2017 7:46 pm

Simple Pulsing LED for a Noob

Post by Virtuous70 »

Hello,

I'm very new to this scene. I'm only guessing that a Trinket MO might be what I need. I just want to create a simple LED circuit where the LED pulses on and off slowly. Can you please advise which product(s) I should purchase? Thanks in advance.

User avatar
kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: Simple Pulsing LED for a Noob

Post by kcl1s »

You do not say what you want to do with the pulsing LED. A one time project? A learning experience? Just for fun?

Do you want it to be battery powered? If so do you want disposable or rechargeable batteries.

Is one LED all you will need? For a couple dollars more you can get a controller with a lot more output pins than the Trinket M0.

Do you currently know any programming languages? Adafruit has controllers that work with C++/Arduino, Circuit Python, and a block based code for beginners called MakeCode.

To just pulse an LED you will need a controller, an LED and a current limiting resistor.

With answers to these questions we can guide you a little better.

Fellow hobbyist
Keith

User avatar
Virtuous70
 
Posts: 23
Joined: Sat Nov 04, 2017 7:46 pm

Re: Simple Pulsing LED for a Noob

Post by Virtuous70 »

Thank you, Keith. This project will be a learning experience for sure, but I'd like it to be a permanent fixture on a cosplay prop. I just need a single LED powered by disposable batteries.
I'm a novice with programming. Pretty comfortable with MakeCode/blocks and recently programmed a traffic light sequence trying to learn a version of C++, so I'm confident I could handle the programming of the device. Is it as simple as writing the code online, then transferring to the chip via USB?
I have 5mm (3V, I think) LEDs... would a 100 Ohm resistor be appropriate?

Thank you so much for your help.
-Chris

User avatar
XRAD
 
Posts: 754
Joined: Sat Nov 19, 2016 3:28 pm

Re: Simple Pulsing LED for a Noob

Post by XRAD »

Trinket M0 is excellent for what you want to do(highly recommended) and it is NOT difficult for you to accomplish. But please read about the programming and all the possibilities in:
https://learn.adafruit.com/adafruit-tri ... o/overview

and since I am a huge fan of everyone trying to bite off a bit more than the average brain can handle, please read about neopixels, the 'awesome version of LED's':
https://learn.adafruit.com/adafruit-neo ... -neopixels

It is VERY simple for you to use C++, and the arduino IDE and program the Trinket M0 to run a single blinking regular LED, BUT it is almost as easy for you to get 1 or 150 neopixels to do color magic.

If you think this is more than you need, the C++ code for a simple one color LED blink is:

Code: Select all

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards (and Trinket M0).

// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}
if you want to turn on and off a neopixel, it will look something like this:

Code: Select all

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket M0
#define PIN    1

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      10// or how many do you have on the strip?

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second

void setup() {
   
  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

  for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGB (red, green, blue) values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }
}
these are both out of the Arduino programmer (IDE) and come with it when you install it. Please read:
https://learn.adafruit.com/adafruit-ard ... p/overview

This is a really cool world of imagine, design, and create. The possibilities are endless.....

User avatar
Virtuous70
 
Posts: 23
Joined: Sat Nov 04, 2017 7:46 pm

Re: Simple Pulsing LED for a Noob

Post by Virtuous70 »

Thank you for all that excellent info, XRAD. The neopixels sound really cool, but this particular prop feature is kind of a retro design, and I think a classic LED is most appropriate. Though you've got me wanting to explore the neopixels for future projects...

I'll go check out those links.

User avatar
Virtuous70
 
Posts: 23
Joined: Sat Nov 04, 2017 7:46 pm

Re: Simple Pulsing LED for a Noob

Post by Virtuous70 »

What power supply (portable battery pack) would you recommend for the Trinket M0?
I have another idea for this circuit board that would have a very small enclosure. Can it power 3 LEDs for brief intervals on something like a CR 2032 battery?

User avatar
XRAD
 
Posts: 754
Joined: Sat Nov 19, 2016 3:28 pm

Re: Simple Pulsing LED for a Noob

Post by XRAD »

really depends on how small your design is. Like a watch case? Toy robot? All you need is a small 1s 3.7v lipo that adafruit sells. But, if you want easy battery recharge, I would suggest that you look at :
https://www.adafruit.com/product/3403

This board is the same processor as the trinket M0, but includes a small lipo charger which plugs right into the adafruit 1S lipo batteries, and charges off the USB. There are a few other tiny lipo charger breakouts that adafruit sell, too.
https://www.adafruit.com/?q=is%20lipo%20charger

You can easily turn on/off a regular LED (I would use a 6v LED and add 100 ohm to whatever ohm resistor if too bright) . However, you can also dim and brighten a regular LED using any of the PWM pins on either board. Be careful! If you decide to purchase a battery pack that is not from adafruit site, look closely at the plug polarity. You will fry the feather lipo charger in about 1.5 seconds with reverse hook-up!

The M0 processors are way more processing power than you would normally need to turn an LED on and off. you can do this with a 555 timer chip or some capacitors/resistors. But you can learn about coding and control your project very easily with the M0 boards. The M0 boards are very cost effective. It is not really worth the time to for you gather the other components and build it....

User avatar
Virtuous70
 
Posts: 23
Joined: Sat Nov 04, 2017 7:46 pm

Re: Simple Pulsing LED for a Noob

Post by Virtuous70 »

I just received my Trinket M0 today and things aren't going very well. The board mounted and performed the demo program with the DotStar cycling through the colors, and pin 3 acting as a touch input for the red LED. Then I opened the main.py file to edit in Notepad++ on Windows 8. After copying and pasting your code above for the simple LED flash, I was not able to replace the existing main.py file on the board. I consistently get "Error 0x80070570 the file or directory is corrupted and unreadable". Even if I save the file to the Desktop and drag it into the Trinket directory, I get the same error.
I tried mounting the Trinket M0 on my Mac, and edited the main.py file with Xcode, and I was able to replace the file successfully, but nothing seems to happen on the board. What should be happening? Should the red LED (13) be flashing?

I tried changing the code from int led = 13; to int led = 3; and connected an LED to pin 3 and Gnd, but nothing happens.
The README says "CircuitPython will automatically load the latest code", but do I need to do anything? Hitting the reset button on the board remounts it.

At this point, when the Trinket is plugged in to a computer, the green light is on, and the DotStar is constant magenta, and nothing else.
Ideas? Thank you in advance.

Here's my modified code, where I've got an LED connected to pin 3 and Gnd.
int led = 3;

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(1500);
}

User avatar
Virtuous70
 
Posts: 23
Joined: Sat Nov 04, 2017 7:46 pm

Re: Simple Pulsing LED for a Noob

Post by Virtuous70 »

I've been trying to learn this all night... made a little progress.
Apparently, I didn't realize I was supposed to double-press the reset button on the board before I transferred the code. I eventually got the results I wanted using blocks at maker.makecode.com, then copied the code over after double-pressing the reset button. It has nothing to do with replacing the main.py file, as I'd been trying to do.

Also, it seems the code suggested above is very different from what they show in their tutorial:
import board
import digitalio
import time

led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

while True:
led.value = True
time.sleep(0.5)
led.value = False
time.sleep(0.5)

And here's the javascript export from my makecode experience:
forever(function () {
pins.D3.digitalWrite(true)
pause(500)
pins.D3.digitalWrite(false)
pause(1500)
})

I'd like to learn how to program the Trinket M0 using text-based coding, but I guess I don't know where to even start. At least I know I can make the board do what I want using the makecode blocks. That's a start.

User avatar
kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: Simple Pulsing LED for a Noob

Post by kcl1s »

It looks like you are getting a little confused with the different languages the Trinket M0 can support. All the languages use the same basic coding concepts (commands, loops, conditionals, variables, functions) but all have a different way of writing it called syntax. They also use different ways of getting the code to the board as you have experienced.

Xrad posted the blink example in Arduino/C++. You have to use the Arduino IDE to compile and load that code.

If you are editing main.py you are coding in Circuit Python. I believe there are issues using Notepad++ with Circuit Python, the suggested editor is the Mu editor.

Makecode/javascript is the third language the Trinket M0 supports. If you can get the results you want with it, I would use Makecode for your project.

It is better to work with one language and get good at it, then try others if you want.

Keith

User avatar
kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: Simple Pulsing LED for a Noob

Post by kcl1s »

I'd like to learn how to program the Trinket M0 using text-based coding, but I guess I don't know where to even start.
Adafruit has lots of learn guides to help you do just that. The guides I link to below will give you a basic understanding of the language and how to get it on the board. Then look at other guides for projects to learn more real world examples.

For Circuit Python this is the place to start https://learn.adafruit.com/welcome-to-circuitpython

For Arduino this is where you would start. https://learn.adafruit.com/experimenter ... -for-metro It is set up for the larger Metro board but most of the tutorial will work with the Trinket M0 if you just change the pin numbers to match what you have available. In the IDE you would choose Adafruit Trinket M0 as your board instead of the Metro.

Keith

User avatar
Virtuous70
 
Posts: 23
Joined: Sat Nov 04, 2017 7:46 pm

Re: Simple Pulsing LED for a Noob

Post by Virtuous70 »

Thanks, Keith. I didn't realize a) the languages were so different, and b) they required different techniques to get them on the board. I will definitely spend some time with the tutorials.
My only remaining question about using Makecode is... when the board mounts on my Mac after I double-press the reset button, I can drag and drop the exported code to the board. However, when I do, there is never an "existing file" visible there. Upon dropping it in the directory, the board automatically unmounts and the code starts working. Do I need to worry about the storage space filling up with all these invisible code files?

User avatar
kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: Simple Pulsing LED for a Noob

Post by kcl1s »

Do I need to worry about the storage space filling up with all these invisible code files?
I have never thought about that. I assume there is some file management going on behind the scene that only allows one file at a time.
I didn't realize a) the languages were so different, and b) they required different techniques to get them on the board.
They each have their advantages and disadvantages.

In the hobby microcontroller realm Arduino/C++ is/was the main player for the past 10 years as the chip (Atmega328) on those boards could not support the newer languages we have now. In Arduino the code is compiled to a machine code on the pc using the IDE, then loaded to the board. This creates code small enough to fit 32 k byte memory on those boards. The disadvantage is every program change has to go through the compile/upload process again which takes a little time.

A couple years ago the M0 chips came on the scene. At first they were just faster chips with more memory that still used the Arduino IDE. Then some people developed Micro Python for these M0 chips. Adafruit created their own version that works with their boards and calls it Circuit Python (btw that is the beauty of Open Source where people can build on what others have started with no hard feelings). Python is an interpreted language which means we can load a 'people' readable file onto the board and a the Python interpreter on the board reads the file line by line and executes the program. A disadvantage is the extra steps slow the code down a bit but it is not noticeable in most cases. Another disadvantage is the amount of memory needed. A big advantage of Circuit Python is that 'people' readable code stays with the chip and can be read and edited on any computer. With the Arduino there is no way to edit the code on the chip. You have to have access to the code on the pc to edit, recompile and upload.

Keith

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”