Munzir Alradi
ع
Munzir Alradi
Home About Services Work Blog Tutorials Contact Light Tunnel العربية
Electronics

Driving Displays, Motors and Addressable LEDs

Getting current, timing and power right when the board has to move something in the real world.

2021 9 min read 31 photos 8 videos Written during Fab Academy · Week 13
Close-up of the ATtiny45 board with jumper leads attached, an Arduino blurred behind it

lets run the programmer i will use Arduino as ISP

Frames 01–02

    Opening the ArduinoISP example from the Arduino IDE Examples menu
    The ArduinoISP sketch open, with the Uno selected on COM12

connect isp with arduino

  • Connect pin 13 from Arduino = pin 7 in Attiny 45 = SCK ISP
  • Connect pin 12 from Arduino = pin 6 in Attiny 45 = MISO ISP
  • Connect pin 11 from Arduino = pin 5 in Attiny 45 = MOSI ISP
  • Connect pin 10 from Arduino = pin 1 in Attiny 45 = RESET ISP
  • Connect pin 5v from Arduino = pin 8 in Attiny 45 = VCC ISP
  • Connect pin GND from Arduino = pin 4 in Attiny 45 = GND ISP

Frames 01–03

    Jumper wires seated in Arduino digital pins 10 to 13
    Leads in the Arduino's digital and power headers, the board in the foreground
    The Attiny board wired to the Arduino over the six ISP lines

if you dont know how to upload the program follow my steps that i have explained in Embedded Programming upload the first example

PWM

Pulse Width Modulation(PWM) is a digital technology that uses the amount of power delivered to a device that can be changed. It generates analogue signals by using a digital source. A PWM signal is basically a square wave which is switched between on and off state. The duty cycle and frequency of a PWM signal determine its behaviour.

On time, off time and the total period marked on a square wave

Duty Cycle in PWM

The duty cycle of the PWM signal refers to the ratio of the time that the signal is in a high(on) state over the total time it takes to complete one cycle. It is commonly expressed as a percentage or a ratio.

Average voltage rising as the duty cycle goes from 25% to 75%

A 50% duty cycle means that the high state takes half of the time and the low state takes the other half of the time, this is the same as an ideal square wave. If this ratio is greater than 50%, the logic high signal takes a longer time than logic low, vice versa. Thus, a 100% duty cycle means the signal is always on(full-scale), and the 0% duty cycle means the signal is always off(grounding).

Frequency of PWM signal

A period is equal to the time this signal completes a one-and-off cycle. The frequency is the number of times a periodic change is completed per unit time and it is the inverse of the period. It determines the speed at which the PWM completes one cycle, which means the speed at which the signal switches between high and low states. If we turn the digital signal on and off repeatedly with a high enough frequency, the output will behave like an analogue signal with a constant voltage.

applying Pulse Width Modulation(PWM)

Fading LED

one of the famous example of applying PWM with Arduino is the Fade Example, you can find it built in on Arduino Ide software, Go To File –» Example –» Basic –» Fade

Picking the Fade sketch from File, Examples, Basics
  • the only modification to make the example run in Attiny45 is to change the led pin

  • number to be realtive to attiny PWM pins

According to the Datasheet (page 27) that Hardware Pwm can be run in PB4-PB0-PB1

Pin Configurations

Frames 01–03

    ATtiny25/45/85 pinout with the Arduino pin numbers alongside each port pin
    The same pinout from the datasheet, for the PDIP, SOIC and TSSOP packages
    And for the QFN/MLF package, where the bottom pad goes to ground

so change the led pin to one of theses, i will choose 0

The led variable changed to 0 in the Fade sketch
  • and dont forget to change the board that you are using
Board set to ATtiny25/45/85 and the processor to ATtiny45

Lets understanf the Code first then we can run it

The three variables at the top of the Fade sketch, marked in red

by understanding that arduino ide is running the code from top to bottom its smart to think as excuter and understand each line only when it time to do action with it.

so at the start we have three variables that have values but it not make sense yet so we have the variables as

  • ” led ” that have the value = ” 0 ”
  • ” brightness ” that have the value = ” 0 ”
  • ” FadeAmount ” that have the value = ” 5 ”

Void setup is one of the tow main function that each aruino ide code need to function properly.

any code will be written in the void setup will be executed only one time

we have pinMode (led,Output); , now we now that the variable led is used here to define the functionality of a pin, this line means that pin 0 will be used as output

The loop, with analogWrite marked as the PWM call and fadeAmount as the increment

Void loop is one of the tow main function that each aruino ide code need to function properly.

any code will be written in the void setup will be executed Forever as long as arduino have power

analogWrite (led,brightness); analogwrite is the way to tell the arduino ide that you need to generate PWM signal, and here now we know that pin 0 will be used to generate a pwm signal with dutycyle = brightness , and brightness is the variable that contain the value of 0 at the start of the program

brightness = brightness + fadeAmount; this line will update the value that stored in the variable brightness with amount of “fadeAmount”, and we know that it contain the value “5”

so that’s mean each time the loop come to this line will add 5 and so on

if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount;} here is a Boolean condition that will run only if the condition is true

and if the condition was true it will execute the code between its brackets, and what’s inside the brackets is fadeAmount = -fadeAmount ; that will change the value of the fadeamount to be either negative or positive by multiplying the value with -1

so here we understand that the led will start from the lowest dutycyle = 0 which = 0V to the highst dutycyle 255 which = 5V

Upload and run the code

Servo

A Servo Motor is a small device that has an output shaft. This shaft can be positioned to specific angular positions by sending the servo a Pulse Width Modulation(PWM) coded signal.

As long as the coded signal exists on the input line, the servo will maintain the angular position of the shaft. If the coded signal changes, the angular position of the shaft changes.

In practice, servos are used in radio-controlled airplanes to position control surfaces like the elevators and rudders. They are also used in radio-controlled cars, puppets, and of course, robots.

A standard servo next to a micro servo with its horns and screws

whats inside the servo ?

Servo is a general term for a closed loop control system.

A closed loop system uses the feedback signal to adjust the speed and direction of the motor to achieve the desired result.

The closed loop: position command in, drive current to the motor, pot feeding position back

RC servo motor works on the same principal. It contains a small DC motor connected to the output shaft through the gears.

The output shaft drives a servo arm and is also connected to a potentiometer (pot).

Inside the case: DC motor, gear train, potentiometer and control unit

The potentiometer provides position feedback to the servo control unit where the current position of the motor is compared to the target position.

According to the error, the control unit corrects the actual position of the motor so that it matches the target position.

How to control Servo Motors with PWM ?

You can control the servo motor by sending a series of pulses to the signal line. A conventional analog servo motor expects to receive a pulse roughly every 20 milliseconds (i.e. signal should be 50Hz).

The length of the pulse determines the position of the servo motor.

Pulses of 1ms, 1.5ms and 2ms every 20ms, and the arm angle each gives

If the pulse is high for 1ms, then the servo angle will be zero. If the pulse is high for 1.5ms, then the servo will be at its center position. If the pulse is high for 2ms, then the servo will at 180 degrees. Pulses ranging between 1ms and 2ms will move the servo shaft through the full 180 degrees of its travel.

Connection

Servo wiring sketched on the photo: GND, VCC and pin 0 of the Attiny45

Programming

i used to test each servo with arduino by running the ready example ” Sweep ”

Loading the Sweep example from the Servo library

but each time i upload the code it give me an error

The Sweep sketch failing to compile for the ATtiny25/45/85 board
servo volatile uint8_t* {aka volatile unsigned char*}' to 'volatile uint16_t* {aka volatile unsigned int*}' for argument '2' to 'void handle_interrupts(timer16_Sequence_t, volatile uint16_t*, volatile uint16_t*)' handle_interrupts(_timer1, &TCNT1, &OCR1A); ^

after a lot of search i found that

The normal Servo.h library does not support the attiny85 (or the tiny861) because these have a very different timer1.

Adafruit SoftServo

so to solve that i found a liberrary made by adafruit called Adafruit_SoftServo

The Adafruit_SoftServo repository on GitHub

you can have it from here

TikerKonb example

Opening TrinketKnob from the Adafruit SoftServo examples

Starting from this example i made the modification that i need , and choosed the pin from the my board to test it on it , and i mixed the code with the sweep example to make it do the same function

The servo sketch, its loop marked as taken from the Sweep example

here is it on Action

WS2812B Addressable LEDs

The WS2812B Addressable LED Strip is an intelligent light source that contains a control IC and an RGB LED in a same package, usually in SMD 5050 form factor. The following image shows a strip of WS2812B LEDs.

A reel of WS2812B strip lit up in mixed colours

Individual WS2812B LED Block has four pins namely VDD, VSS (GND), DIN and DOUT. The functions of these pins are very simple and the following table gives a simple function description of the pins.

Pin functions of one WS2812B block: VDD, VSS, DIN and DOUT

To understand how the WS2812B LEDs can be individually controlled, the following application circuit will be useful. All the WS2812B LEDs are connected in a cascade manner where the DO of the first LED is connected to the DIN of the second LED and so on.

But the first LED has to receive data through its DIN pin from a Microcontroller like Arduino. The following image shows the typical application circuit block diagram.

Three WS2812B blocks cascaded, the microcontroller feeding DIN of the first and each DO carrying on to the next

The data transfer protocol used by the WS2812B LEDs is NRZ Mode. The first DIN Port of the WS2812B LED array receives data from the microcontroller. The data for individual pixel is of 24-bit that consists of individual Red, Green and Blue LED control data of 8-bits each. The order of data must be GRB and the composition of the 24-bit data is shown below. Note that HIGH bit data is sent first.

The 24-bit pixel laid out green first, then red, then blue

Once the first WS2812B Block receives the first 24-bit data, the data is sent to its internal latch for further decoding. The remaining data is reshaped by its signal reshaping and amplification circuit and is passed to the next pixel in the cascade through the DO pin.

Connection

The strip leads labelled GND, pin 0 and VCC at the connector

LED —- Attiny45

Red —- »> 12 V White —- »> GND Green —- »> pb0
GND —- »> GND

Make sure to connect the gnd to together so the signal will stable

i used the liberrary FastLED to sontrol the strip light

Problem

the first problem was that the memory was not enough in the attiny45

Sketch too big: the FastLED ColorPalette example overflowing the Attiny45
  • second problem was that all leds on the strip gets light up during the code uplad, and they never turn off even if i write for them color black
  • and the code is not responding

Solved

to solve these issue

  • i reduced the number of leds to be controlled
  • i cut the rest that is not in the program

with sequencing

faster

Attiny85

i found that Attiny85 has more memry than attiny45 so i gave it a test to see if it can drive more leds

Download

Reference

The Fab Academy brief

Group assignment:

  • Measure the power consumption of an output device
  • Document your work (in a group or individually)

Individual assignment:

  • Add an output device to a microcontroller board you’ve designed and program it to do something

Type to search