Différences entre versions de « NeoPixel-UserGuide-Arduino »

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche
Ligne 29 : Ligne 29 :
  
 
{{ADFImage|NeoPixel-UserGuide-Arduino-Brancher-01.png|640px}}
 
{{ADFImage|NeoPixel-UserGuide-Arduino-Brancher-01.png|640px}}
 +
 +
{{ambox-stop|text=When connecting NeoPixels to any live power source or microcontroller, ALWAYS CONNECT GROUND (–) BEFORE ANYTHING ELSE. Conversely, disconnect ground last when separating.}}
 +
 +
 +
{{ambox-stop|text=Adding a ~470 ohm resistor between your microcontroller's data pin and the data input on the NeoPixels can help prevent spikes on the data line that can damage your first pixel. Please add one between your micro and NeoPixels!}}
 +
 +
 +
{{ambox-stop|text= We also recommend adding a large capacitor (1000 µF, 6.3V or higher) across the + and – terminals. This prevents the initial onrush of current from damaging the pixels. See the photo on the next page for an example.}}
 +
 +
=== Can NeoPixels be powered directly from the Arduino’s 5V pin? ==
 +
 +
Sometimes. The Arduino can continuously supply only about 500 milliamps to the 5V pin. Each NeoPixel can draw up to 60 milliamps at full brightness. So yes, you can skip the separate DC supply and power directly off the Arduino as long as just a few pixels are used, or more if the colors and overall brightness are low. When in doubt, give the pixels a separate power supply.
 +
 +
== Code d'exemple simple: standtest ==
 +
 +
Launch the Arduino IDE. From the File menu, select '''Fichier > Exemple > Adafruit_NeoPixel > strandtest'''
 +
 +
(If the Adafruit_NeoPixel rollover menu is not present, the library has not been correctly installed, or the IDE needs to be restarted after installation. Check the installation steps above to confirm it’s properly named and located.)
 +
 +
Select your board type and serial port from the Tools menu, and try uploading to the board. If the NeoPixels are connected and powered, you should see a little light show.
 +
 +
=== Il ne se passe rien ===
 +
 +
Check your connections. The most common mistake is connecting to the output end of a strip rather than the input.
 +
 +
=== Inspectons le code ===
 +
 +
Let’s look at the code now...
 +
 +
All NeoPixel sketches begin by including the header file:
 +
 +
<nowiki>#include <Adafruit_NeoPixel.h></nowiki>
 +
 +
The block of code that follows is mostly descriptive comments. Only the last line is really doing any work:
 +
 +
<nowiki>#define PIN 6
 +
 +
// Parameter 1 = number of pixels in strip
 +
// Parameter 2 = pin number (most are valid)
 +
// Parameter 3 = pixel type flags, add together as needed:
 +
//  NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
 +
//  NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
 +
//  NEO_GRB    Pixels are wired for GRB bitstream (most NeoPixel products)
 +
//  NEO_RGB    Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
 +
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);</nowiki>
 +
 +
The first line assigns a number to the symbol “PIN” for later reference. It doesn’t ''need'' to be done this way, but makes it easier to change the pin where the NeoPixels are connected without digging deeper into the code.
 +
 +
The last line declares a NeoPixel ''object''. We’ll refer to this by name later to control the strip of pixels. There are three parameters or arguments in parenthesis:
 +
# The number of sequential NeoPixels in the strip. In the example this is set to 60, equal to 1 meter of medium-density strip. Change this to match the actual number you’re using.
 +
# The pin number to which the NeoPixel strip (or other device) is connected. Normally this would be a number, but we previously declared the symbol PIN to refer to it by name here.
 +
# A value indicating the type of NeoPixels that are connected. '''In most cases you can leave this off and pass just two arguments'''; the example code is just being extra descriptive. If you have a supply of classic “V1” Flora pixels, those require NEO_KHZ400 + NEO_RGB to be passed here.
 +
 +
Then, in the setup() function, call begin() to prepare the data pin for NeoPixel output:
 +
 +
<nowiki>void setup() {
 +
  strip.begin();
 +
  strip.show(); // Initialize all pixels to 'off'
 +
}</nowiki>
 +
 +
The second line, strip.show(), isn’t absolutely necessary, it’s just there to be thorough. That function pushes data out to the pixels…since no colors have been set yet, this initializes all the NeoPixels to an initial “off” state in case some were left lit by a prior program.
 +
In the strandtest example, loop() doesn’t set any pixel colors on its own — it calls other functions that create animated effects. So let’s ignore it for now and look ahead, inside the individual functions, to see how the strip is controlled.
 +
 +
There are two ways to set the color of a pixel. The first is:
 +
 +
<nowiki>strip.setPixelColor(n, red, green, blue);</nowiki>
 +
  
 
{{NeoPixel-UserGuide-TRAILER}}
 
{{NeoPixel-UserGuide-TRAILER}}

Version du 24 février 2014 à 14:33


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

Bibliothèque Arduino

Controlling NeoPixels “from scratch” is quite a challenge, so we provide a library letting you focus on the fun and interesting bits. The library works with most mainstream Arduino boards and derivatives: Uno, Mega, Leonardo, Micro, Adafruit Flora, etc. — most anything with an Atmel AVR 8-bit processor from 8 to 16 MHz — and also works with the Arduino Due and all varieties of the PJRC Teensy boards.

Because processor-specific assembly language is used, this library does not work on Netduino, ChipKIT or other advanced “Arduino-like” boards. Others may have written code and libraries for such boards, but we can’t provide technical support for any bugs or trouble there; that’s frontier stuff. Some of this is covered in the “Advanced Coding” section.

Installation of the library is as follows:

  • Visit the Adafruit_NeoPixel library page at Github.com.
  • Select the “Download ZIP” button, or simply click this link to download directly.
  • Uncompress the ZIP file after it’s finished downloading.
  • The resulting folder should contain the files “Adafruit_NeoPixel.cpp”, “Adafruit_NeoPixel.h” and an “examples” sub-folder. Sometimes in Windows you’ll get an intermediate-level folder and need to move things around.
  • Rename the folder (containing the .cpp and .h files) to “Adafruit_NeoPixel” (with the underscore and everything), and place it alongside your other Arduino libraries, typically in your (home folder)/Documents/Arduino/Libraries folder. Libraries should not be installed alongside the Arduino application itself.
  • Re-start the Arduino IDE if it’s currently running.

Here’s a tutorial that walks through the process of correctly installing Arduino libraries.

Branchement de base

To get started, let’s assume you have some model of Arduino microcontroller connected to the computer’s USB port. We’ll elaborate on the finer points of powering NeoPixels later, but for now you should use a separate 5V DC power supply (or a 3.7V lithium-ion battery for a Flora wearable project).

Identify the “input” end of your NeoPixel strip, pixel(s) or other device. On some, there will be a solder pad labeled “DIN” or “DI” (data input). Others will have an arrow showing the direction that data moves. The data input can connect to any digital pin on the Arduino, but all the example code is set up for digital pin 6 by default. The NeoPixel shield comes wired this way.

If using Flora with an attached lithium-ion battery: connect the +5V input on the strip to the VBATT pad on Flora, GND from the strip to any GND pad on Flora, and DIN to Flora pin D6.

For other Arduino boards with a separate +5V DC power supply for the NeoPixels: connect the +5V input on the strip to the + (positive) terminal on the power supply (don’t connect to the Arduino), DIN to digital pin 6 on the Arduino, and – (minus or GND) on the strip must connect to both the minus (–) terminal on the DC supply and a GND pin on the Arduino (there are usually several — any will do).

The 144 pixel strips are so tightly packed, there’s no room for labels other than –, + and the data direction arrows. Data in/out is the un-labeled pad.

NeoPixel-UserGuide-Arduino-Brancher-01.png
Crédit: AdaFruit Industries www.adafruit.com



= Can NeoPixels be powered directly from the Arduino’s 5V pin?

Sometimes. The Arduino can continuously supply only about 500 milliamps to the 5V pin. Each NeoPixel can draw up to 60 milliamps at full brightness. So yes, you can skip the separate DC supply and power directly off the Arduino as long as just a few pixels are used, or more if the colors and overall brightness are low. When in doubt, give the pixels a separate power supply.

Code d'exemple simple: standtest

Launch the Arduino IDE. From the File menu, select Fichier > Exemple > Adafruit_NeoPixel > strandtest

(If the Adafruit_NeoPixel rollover menu is not present, the library has not been correctly installed, or the IDE needs to be restarted after installation. Check the installation steps above to confirm it’s properly named and located.)

Select your board type and serial port from the Tools menu, and try uploading to the board. If the NeoPixels are connected and powered, you should see a little light show.

Il ne se passe rien

Check your connections. The most common mistake is connecting to the output end of a strip rather than the input.

Inspectons le code

Let’s look at the code now...

All NeoPixel sketches begin by including the header file:

#include <Adafruit_NeoPixel.h>

The block of code that follows is mostly descriptive comments. Only the last line is really doing any work:

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

The first line assigns a number to the symbol “PIN” for later reference. It doesn’t need to be done this way, but makes it easier to change the pin where the NeoPixels are connected without digging deeper into the code.

The last line declares a NeoPixel object. We’ll refer to this by name later to control the strip of pixels. There are three parameters or arguments in parenthesis:

  1. The number of sequential NeoPixels in the strip. In the example this is set to 60, equal to 1 meter of medium-density strip. Change this to match the actual number you’re using.
  2. The pin number to which the NeoPixel strip (or other device) is connected. Normally this would be a number, but we previously declared the symbol PIN to refer to it by name here.
  3. A value indicating the type of NeoPixels that are connected. In most cases you can leave this off and pass just two arguments; the example code is just being extra descriptive. If you have a supply of classic “V1” Flora pixels, those require NEO_KHZ400 + NEO_RGB to be passed here.

Then, in the setup() function, call begin() to prepare the data pin for NeoPixel output:

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

The second line, strip.show(), isn’t absolutely necessary, it’s just there to be thorough. That function pushes data out to the pixels…since no colors have been set yet, this initializes all the NeoPixels to an initial “off” state in case some were left lit by a prior program. In the strandtest example, loop() doesn’t set any pixel colors on its own — it calls other functions that create animated effects. So let’s ignore it for now and look ahead, inside the individual functions, to see how the strip is controlled.

There are two ways to set the color of a pixel. The first is:

strip.setPixelColor(n, red, green, blue);



Source: NeoPixel UserGuide créé par Phillip Burgess pour AdaFruit Industries. Crédit AdaFruit Industries

Traduit par Meurisse D. pour MCHobby.be

Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.