Categories
ELECTRONICS EMBEDDED EMBEDDED COMPUTERS EMBEDDED PROGRAMMING IOT PROGRAMMING RASPBERRY PI TUTORIALS

WS2811 Vs WS2812 Difference In LED Strip

Are you planning to connect LED strips to your board? Then you would want to know the difference between WS2811 Vs WS2812. So read below to know more!

Most of the articles on Arduino or Raspberry Pi asks you to hook up an WS2811 or WS2812 LED strip to your board. But have you ever tried to know why these LED strips? Why not any other LED strips? What makes them so special? And what about the WS2811 Vs WS2812 Difference?

WS2811 Vs WS2812 Difference In LED
WS2811 Vs WS2812 Difference In LED

Not many tutorials out there have tried to answer these questions. So in this article, we will do just that! So brace yourself. Sit tight and join the ride to learn all about these LED strips. Alright? Cool! Here we go!

What Is An LED Strip?

So before we dwell into learning the WS2811 Vs WS2812 Difference, let us try to answer this question first.

What is an LED?

An LED is a simple semiconductor device that will emit light when electric current flows through it. Now notice how I used the term “semiconductor device“. But what type of device is it? Well to be precise, it is a semiconductor diode.

So in short, LED stands for Light Emitting Diode. So what do we mean by that? It means that when electricity passes through this diode, it emits light!

So now that we know what an LED is, it is time to answer our next question.

What Is WS2811 LED Strip?

A WS2811 LED strip uses WS2811 LED driver chip to control the LED strip. Uhh… Got confused by what I just said?

Well, let me clear that for you by answering this question.

What is an LED strip?

You see, an LED strip is just a fancy way of connecting a bunch of LED lights together along a strip of wire. So an LED strip will have more than one LED lights in them.

But what makes them great is that you can address each of these LED lights separately.

Wait what? How can we do that, you ask?

Well that is where LED driver IC like WS8211 comes into picture. It is these chips that will help us connecting to each of the LED and controlling them to turn then ON or OFF.

Are you with me until here? Great! Then let us find answer to the next question

How Does WS8211 Chip Control LED?

So here is the thing. To control individual LEDs in a strip, LED driver chips like WS8211 is used. These chips sit alongside the LED and control their light intensity.

So by makes use of serial protocol in your microcontroller and assigning unique addresses to each of the LED driver chip you can control each of the LEDs.

So this way, when you want to control an LED, your board’s microcontroller will send out the address of the LED driver chip to be controlled. This will make the respective LED chip accept the next set of commands to be sent for itself.

And this way, your board will be able to control every LED to be turned ON or OFF!

Pretty neat, isn’t it? 😉

So now that we know what an LED driver chip like WS8211 does, it is finally time to ask the important question of this article.

WS2811 Vs WS2812 Difference

While both WS2811 & WS2812 are LED controller ICs, there are a few notable differences between them. Here are few of them that we listed out for you:

WS2811 Vs WS2812 Difference In Power

WS2811 LED chip uses 12V to work. Where as a WS2812 chip which uses only 5V. But then how will this impact you?

Well you see, if you are going to use along LED strip with many LEDs in them, you are going to see quite a bit of voltage drop along the strip.

Which means that the last few LEDs that is present at the far end of the strip from the board will get lower voltage. Now, if this drop is significant, your last few LEDs might not be glowing as well as its should be.

You do not want that. Right?

So for example you have 30 LEDs in your strip and the voltage drop is 2V towards the end of the strip. Well in that case, if you are using a WS2811 chip, the last few LEDs will get around 10V. This is still reasonably OK for an LED supposed to operate at 12V.

But on the other hand, if you are using WS2812 that works at 5V. the same voltage drop will be down to 3V from the supposed 5V. Now that is quite a significant voltage drop there. Right?

So you need to keep this in mind while choosing between WS2811 Vs WS2812.

WS2811 Vs WS2812 Difference In LEDs Controlled

WS2811 actually do not control one LED. But instead it controls a cluster of 3 LEDs together. So that means that you cannot go below a granularity level of 3 LEDs to control them.

But on the other hand, WS2812 can control 1 LED each. So, if you need a control granularity of 1 LED, you should be going for WS2812 instead of WS2811.

WS2811 Vs WS2812 Difference In Power Consumption

As WS8211 operates at 12V, the amount of power consumed by it is significantly more. On the other hand, as WS8212 uses 5V, it can operate at a much lower power consumption.

WS2811 Vs WS2812 – Pros & Cons Of Each

WS8211 Pros

  • Operates at 12V. So it can control more number of LEDs in a strip.
  • Ease of use when dealing with mass LED controls.

WS8211 Cons

  • It cannot control lower than 3 LEDs at a time. So its lower granularity level of control lies at 3 LEDs.
  • Consumes significantly higher power because of 12V of operation.

WS8212 Pros

  • Can control single indiviudal LEDs.
  • LED control graularity lies at 1 LED.
  • Lower power consumption.

WS8212 Cons

  • As it operates at 5V, its voltage drop becomes significant as the number of LEDs in the strip increases.

Conclusion

So in conclusion, whether to choose WS2811 or WS2812 entirely depends upon the type of use cases you are dealing with. If you want to control a long LED strip then WS2811 is the obvious choice for you.

But on the other hand, if you want to control LEDs at the granular level of a single LED, then WS2812 is the option for you to go for.

So hope this article was helpful for you in understanding the WS2811 Vs WS2812 difference. If you still have any more questions in your mind, do let me know in the comments below and I will try to give you my best help.

So with that, I will end this article here. Until next time, take care and happy learning! 🙂

Categories
ARM ARM ARCHITECTURE ARM PROGRAMMING COMPUTER HARDWARE ELECTRONICS EMBEDDED EMBEDDED COMPUTERS EMBEDDED PROGRAMMING HARDWARE PROGRAMMING PYTHON RASPBERRY PI RASPBERRY PI PROJECTS TUTORIALS

Blinking An LED Connected To GPIO Pin Of Raspberry Pi Using Python

Introduction

If you are just getting started with Raspberry Pi, connecting a simple LED to one of the GPIO pins of a Raspberry Pi and controlling it using software program that you write will give you a very good grasp of how a computer hardware and its program works internally. You will realize how you can control various aspects of a computer hardware using software, how a computer works at the bit level, how to write Python programs to control hardware and more.

In summary, working on getting an led connected to a GPIO pin of your Raspberry Pi will help you in understanding the fundamentals of a computer architecture and computer science in general.

Raspberry Pi 3B

What You Will Learn From This Project?

Connecting an LED to the GPIO pins of a Raspberry Pi to control it is a simple Beginner Raspberry Pi Project that lets you learn more about:

  • Raspberry Pi hardware internals
  • General Purpose Input/Output (GPIO) pins of a Raspberry Pi
  • Raspberry Pi Register Set
  • Ohm’s Law
  • Python Programming
  • Python Library – Raspberry Pi GPIO library
  • The working of an Light Emitting Diode (LED)

What Hardware Is Required To Set Up A Blinking LED Project?

This a very simple, beginner friendly Raspberry Pi project that can be set up by anyone with minimal hardware or software knowledge. The hardware components required to set up this blinking LED project is also quite minimal. You need the following hardware components available with you to get it going:

  • Raspberry Pi Module
  • Solderless Breadboard
  • Keyboard
  • Monitor
  • Raspberry Pi Power Supply
  • SD Card with working Raspbian OS
  • Jumper wires for rigging up the circuit
  • LED
  • Resistor (1K Ohm)
  • Multimeter

Theory Behind How The Raspberry Pi Blinking LED Project Work

When you look at the Raspberry Pi board, you will see a bunch of pins protruding out. Among these, there is a row of 40 pins located on one side of the board as shown in the image below.

If you look closely enough in the above image, you will notice the label “GPIO” written right under it. These pins are called the GPIO pins or General Purpose Input Output pins. What the name GPIO implies is that these pins do not have any fixed functionality within the board and hence can be used for general purposes. It means that we can connect our LED into one of these pins and can turn it ON or OFF using these pins. But how?

How to control the Raspberry Pi GPIO pins programmatically?

Raspberry Pi 3 board runs on Broadcom’s ARM CPU chipset BCM2837. Among many other things, this processor chipset has a built in GPIO controller aka General Purpose Input Output controller. The 40 GPIO pins header shown in figure 1 is connected to 40 controllable pins of the GPIO controller. Now, we can control each of these pins individually by programming the appropriate registers inside this GPIO controller.

To understand how to program each of these pins using GPIO controller, we need to look into the Technical Reference Manual or datasheet of the Broadcom ARM chipset BCM2837.

In the BCM2837 SOC (System On Chip aka CPU) datasheet linked above, if we jump into page 89 we come across a dedicated chapter talking about General Purpose Input Output (GPIO). If we go through this chapter, we can learn about all the GPIO registers available and figure out the GPIO registers we need to program to turn ON or OFF the LED we are going to connect to the Raspberry Pi 3 GPIO pins.

As the name implies, GPIO pins can be configured as either an Input pin or an Output pin. When we configure a GPIO pin as an input pin, we are sending data bit (either 0 or 1) into the Raspberry Pi BCM2837 SOC i.e. data signal is sent from outside the board to inside the board (hence the name input). On the other hand, if we configure the Raspberry Pi GPIO pin as an output pin, the board will send the data bit signal (either 0 or 1) from inside the board to the outer world where any device connected to it will receive this signal.

So, if we want to control an LED that is connected to one of the Raspberry Pi’s GPIO pin, we need to configure that pin as a GPIO OUT pin (aka output pin) so that we can send an electrical signal from the Raspberry Pi board to the external LED connected to this pin.

The configuration of a GPIO pin to be an INPUT or OUTPUT pin is controlled by programming the GPIO Controller Register called GPIO Function Select Register (GPFSELn) where n is the pin number.

So for example, if we choose to use the GPIO8 pin to control the LED, i.e. we connect our LED to GPIO 8 pin, we need to program the GPFSEL register for the GPIO 8 pin and configure it as an Output pin. When we check the datasheet at page 91 and 92, we notice that GPIO pin is configured by setting the bits 26 to 24 in the GPFSEL register (that is field name FSEL8). And from the datasheet, we also find that to set the pin as an output pin, we need to set its value as 001 i.e. bit 26 is set to 0, bit 25 is set to 0 and bit 24 is set to 1.

So, if we can somehow set these values in the GPFSEL register using a programming language such as Python, we will be able to start controlling the LED connect to this pin!

If this is all overwhelming to you, do not worry. We will not have to scratch our head a lot for now as we can simply make use of Raspberry Pi’s GPIO Python library that helps us in making most of this work for us. But I just wanted to explain to you as to what this GPIO Python library is doing under the hood.

How To Connect An LED To Raspberry Pi GPIO?

Designing The Circuit

In order to connect an LED to GPIO pin 8 of Raspberry Pi, we need to first design and understand how the circuit is going to work.

Can we connect an LED directly to a Raspberry Pi GPIO pin without a resistor?

The answer is No. Raspberry Pi provides 3.3 Volts of power on its GPIO output pin according to Raspberry Pi datasheet specification. However, if we take a look at a standard LED, we notice that it normally operates at a much lower voltage. If we look at an LED specification, we notice that a typical LED usually operates at just 1.7 Volts and draws 20 mA. So, if we need to connect this LED to the GPIO pin of our Raspberry Pi, we need to bring down the voltage delivered by the pin to our LED to operate at or under 1.7V. How to do that? We connect a resistor in series with our LED so that the 3.3 Volts GPIO output of Raspberry Pi gets split between the resistor and our LED. By choosing a right value of the resistor such that it consumes 1.6 Voltage, we can ensure that LED finally gets only 1.7 Volts.

Calculating the resistor value to connect with LED and Raspberry Pi GPIO

In order to calculate the value of resistor that we should be using, we make use of the Ohm’s Law.

Ohm’s Law is defined using the equation:

V = I/R where V is the voltage, I is the current and R is the resistor value.

So, if we want to have V=1.6 Volts consumed by our resistor so that the current coming from GPIO pin is at I=20 mA, we need to connect a resistor whose value is:

1.6 = (20 mA)/R

or R = 80 Ohms (or approx 100 Ohms)

So, we choose a resistor of value 100 Ohms connected in series with our resistor to ensure that we only get 1.7 Volts and 20 mA of current, the optimum operating values as required by our LED.

A 100 Ohm resistor is identified by the color bands: Brown, Black & Brown.

Hook up the led through 100 Ohm resistor to GPIO 8 pin of Raspberry Pi as shown in the figure below:

Note that when you are hooking up the LED, the terminal pin that is longer is positive. Once you have connected as shown in the figure, it is now time to program the Raspberry Pi GPIO controller to start controlling the LED to turn ON or OFF.

We will be using Python to program our Raspberry Pi GPIO controller. Now, the simplest way to program this is by making use of the Python GPIO library.

To install the Python Raspberry Pi GPIO module, open up your linux terminal and type the following command.

sudo apt-get install python-rpi.gpio python3-rpi.gpio

Now the above command will install the required Python GPIO library module onto our Linux development machine. Once successfully installed, It is now time to start programming the Raspberry Pi GPIO controller.

We will be toggling our GPIO pins at 1 second intervals such that our LED will turn ON and OFF forever until the Python program we write will be terminated i.e., we will be running the code to perform infinite loop of toggling the GPIO 8 pin ON and OFF.

Create a new file on your computer by typing the following command in the terminal:

touch blinky.py

This should create our new program file called blinky.py

Open up this file using nano editor by typing the following command in the terminal:

nano blinky.py

Now that the file is opened, it is time to start writing our program to control the GPIO Pin 8 using Python GPIO library module.

First thing first, we will import the Python GPIO library module using command:

import RPi.GPIO as GPIO

Next, we will import python time library to perform 1 sec sleep operation between each GPIO toggle

from time import sleep

Next, we need to configure our GPIO library to use our GPIO physical pin numbering as seen on the Raspberry Pi board physically:

GPIO.setmode(GPIO.BOARD)

This ensures that when we say GPIO pin 8 in the program, it actually maps to the GPIO Pin 8 seen on the Raspberry Pi board.

Next, configure GPIO pin 8 to be a GPIO Out pin and set its initial output value to be low:

GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW)

Finally we will start an infinite loop in Python such that we turn ON the GPIO 8 (by setting it HIGH) or turn it OFF (by setting it LOW) after every 1 second delay. This is achieved using the program below:

while True: # Infinite loop
    GPIO.output(8, GPIO.HIGH) # Turn GPIO 8 pin on
    sleep(1)                  # Delay for 1 second
    GPIO.output(8, GPIO.LOW)  # Turn GPIO 8 pin off
    sleep(1)                  # Delay for 1 second

That’s it, this should be all the program that we need to type in our blinky.py file and run it using the command:

python3 blinky.py

This should start turning your LED ON and OFF every second!

Here is the full code for your reference:

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW)

while True: # Infinite loop
    GPIO.output(8, GPIO.HIGH) # Turn GPIO 8 pin on
    sleep(1)                  # Delay for 1 second
    GPIO.output(8, GPIO.LOW)  # Turn GPIO 8 pin off
    sleep(1)                  # Delay for 1 second

This should conclude our tutorial on how to get a simple LED connected to a General Purpose Input/Output (GPIO) pin turning ON and OFF using a Python program that makes use of Python Raspberry Pi GPIO library. There can be many variants to this such as using other GPIO pins, connecting more than one LEDs to multiple GPIO pins and controlling them all in different ways to display interesting patterns on the LEDs. If we are even more curious, we can also figure out a way to control the BUILT-IN LEDs that are already present on our Raspberry Pi boards to bypass their current usage and be used for by own programs for our purposes.

We will dwell into these and many other interesting ways to make use of our Raspberry Pis to understand and learn more about the computer hardware, its architecture and much more in our future articles.