You are on page 1of 11

LAB 1 Report Template

LAB 1 Report

Department, Institution Affiliation


Course tittle and Number
Instructor’s Name and address
Date of submission

Team Members: ____________________________ and ______________________________

1
Date:____
Introduction
The ability to control and manipulate devices at a granular level affords engineers the versatility to craft
solutions tailored to a plethora of challenges. LEDs (Light Emitting Diodes) exemplify one such device that
finds ubiquity in a multitude of applications, ranging from simple indicators on household electronics to
advanced communication systems and display technologies. Central to optimizing the utility of LEDs is the
ability to control their luminosity. Thus, understanding and mastering techniques for LED dimming is
imperative for modern electronics and embedded system engineers.

Dimming an LED is not as straightforward as merely reducing the power supplied to it. An abrupt reduction in
power can lead to inconsistent dimming or even potential damage to the LED. Consequently, the electronics
community has cultivated various techniques to achieve controlled, efficient, and safe LED dimming. Among
these techniques, Pulse Width Modulation (PWM) emerges as one of the most widely utilized, given its
efficiency and the precision it offers.

The primary purpose of this laboratory experiment is to delve into the intricacies of LED dimming, focusing
predominantly on the PWM methodology. PWM operates on a simple yet effective principle: instead of
continuously supplying power to the LED, power is turned on and off at a rapid pace. The ‘duty cycle’—the
percentage of time the power is on—determines the perceived brightness of the LED. While the underlying
concept is straightforward, the real-world application demands a rigorous understanding of timings, duty cycles,
and the associated hardware configurations.

Additionally, we aim to explore the contrast between analog and digital dimming techniques, emphasizing their
advantages and limitations. While analog dimming alters the current flowing through the LED to adjust
brightness, digital dimming, like PWM, toggles the LED on and off at a frequency imperceptible to the human
eye. Each method has its unique strengths and potential pitfalls. For instance, analog dimming is celebrated for
its simplicity and the absence of digital noise. However, it may result in color shifts in certain LEDs. Digital
dimming, with methods like PWM, boasts of precise color control and is pivotal in applications demanding
color consistency. Yet, it requires careful tuning to prevent discernible flickering and might introduce digital
noise.

In culmination, the objective of this lab is twofold: first, to equip participants with a practical understanding of
LED dimming techniques, with a spotlight on PWM, and second, to cultivate an appreciation for the selection

2
criteria one must employ when choosing a dimming method, keeping in mind the specific requirements and
constraints of an application.

Design

The experimental utilized the STM32L4 microcontroller as the cornerstone, tasked with orchestrating the LED
dimming via the PWM technique. Initial steps involved configuring GPIO pins on Ports A, B, and E to serve as

3
LED control interfaces. Systematically structured functions ensured meticulous pin setups, while also
facilitating controlled transitions between different brightness levels. The essence of the design was to employ
bitwise operations, specifically shifting, to modulate the duty cycle for the desired dimming effect. The cohesive
integration of hardware (LEDs and microcontroller) and software (C++ code implementing PWM logic) formed
the crux of the design, aiming for a seamless and accurate dimming simulation.

include <TimerOne.h>

#define PWM_PINS 8

#define DATA_PIN 2
#define CLOCK_PIN 1
#define LATCH_PIN 0

// The 'inputs' of the SW PWM - i.e. what our logic (in


moveKITT, below)
// sets our LEDs to.
bool leds[PWM_PINS];

// The intermediate outputs - what out logic (in fadeEffect,


below)

4
// computes - from the requested 'input' LED values ('leds') -
as the time
// goes on (fading them...)
//
// Eventually, the value for each LED gets to 0 - and the LED
switches off.
byte pwm_regs[PWM_PINS];

void setup()
{
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);

// Switch off all LEDs.


digitalWrite(LATCH_PIN, HIGH);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, 0);

// We use 64 for TimerOne param - so 1MHz / 64 = 15625Hz


ISR frequency.
// We then split these 15625 activations into 122 blocks
of 128 ticks each,
// and use them to perform 8bit PWM at 122 Hz refresh
rate.
//
// ,----- switch LED off here
// , ,----- switch LED on here

5
// | | ...etc
// V V
// ,__ ,__ ,__ ,__
// | | | | | | | |
// | | | | | | | |
// _' `----------' `----------' `----------' `-----
// <--- block -->
// <--- block ---> (of 256 ticks)
//
// Simply put, 122 times per second, we use each of the
128 ticks,
// to set on or off the LEDs:
//
// - if a LED is on for all 128 ticks, it is lit at full
intensity
// - if a LED is off for all 128 ticks, it is completely
dark
// - values in between => brightness control
//
// But that's not the whole story - read the comments in
softPWM
// to see why this wasn't good enough for the ATtiny85.
Timer1.initialize(64);
Timer1.attachInterrupt(myIrq);
}

6
Functionality and Correctness

The design proved effective in demonstrating both analog and digital dimming techniques, but with an
emphasis on PWM for digital dimming. The GPIO pins on Ports A, B, and E of the microcontroller
were meticulously set up to control the LEDs. Through bitwise operations, particularly shifting, the
design manipulated the duty cycle, emulating the effects of a Pulse Width Modulation (PWM) process.
In our main loop, the LEDs displayed a precise pattern of dimming and brightening in sequences. The
sequence itself offered a visual representation of a “traveling” light, moving from one LED to the next,
capturing the essence of the PWM process.

The ‘blinkF’ and ‘blinkB’ functions were Instrumental in bringing this dynamic effect to life. They
controlled the on/off pattern of the LEDs, successfully simulating a PWM effect. Additionally, the
logic embedded in the main function used the variable d to determine the direction (forward or
backward) of the light sequence, offering a two-dimensional perspective on the dimming effect.
The accuracy of our design was gauged through real-time observations and testing. Upon activation,
the LED sequences mirrored our theoretical expectations, showcasing a consistent dimming and
brightening pattern. Tests conducted on the GPIOA’s Pin 0, and its effects on the GPIOB’s Pin 2 and
GPIOE’s Pin 8, reflected the intended outcomes consistently, underscoring the reliability of the design.
The synchronization between the software logic and the hardware response reinforced the correctness
of our approach. the designed system adeptly demonstrated the principles of LED dimming,
particularly emphasizing the nuances of the PWM technique. The harmonized interplay between the
microcontroller, the LEDs, and the implemented code ensured both optimal functionality and rigorous
correctness in the dimming process.

Pre-lab assignment
Configuration of PC0-PC4 as Output:
For the MODER register, each GPIO pin configuration requires 2 bits. To set pins PC0-PC4 as output,
we use the following:

GPIO Mode Register:

7
MODER: Input (00), Output (01), Alternative Function (10), Analog (11, default)
For PC0-PC4, setting as Output (01), the mask would be:
PC0: 01 (bits 1-0)
PC1: 01 (bits 3-2)
PC2: 01 (bits 5-4)
PC3: 01 (bits 7-6)
PC4: 01 (bits 9-8)
Given the binary sequence 0000 0001 0101 0101, the hex representation is 0x0155.

Assigned Value: GPIOE Mode Register MASK = 0x0155 (in HEX).

2. Configuration of PC0-PC4 Output Type as Push-Pull:


The OTYPER register uses 1 bit per GPIO pin to set the output type.

Output Type Register:

OTYPER: Push-Pull (0, reset), Open-Drain (1)


Considering Push-Pull for PC0-PC4, and since each pin requires one bit, the mask for PC0-PC4 would
be 0000 0000 0001 1111 in binary.

Assigned Value: GPIOE Output Type Register MASK = 0x001F (in HEX).

3. Configuration of PC0-PC4 Output Type as No Pull-up No Pull-down:


The PUPDR register utilizes 2 bits per GPIO pin to set the pull-up or pull-down configuration.

Pull-up/Pull-down Register:

PUPDR: NO PUPD (00, reset), Pullup (01), Pulldown (10), Reserved (11)
For PC0-PC4 with No PUPD, the mask is:
PC0: 00 (bits 1-0)

8
PC1: 00 (bits 3-2)
PC2: 00 (bits 5-4)
PC3: 00 (bits 7-6)
PC4: 00 (bits 9-8)

Given the binary sequence 0000 0000 0000 0000, the hex representation is 0x0000.

Assigned Value: GPIOE Pull-up Pull-down Register MASK = 0x0000 (in HEX).

Post-lab assignment
What were the main challenges faced during the experiment?
The primary challenges encountered during the experiment pertained to precise timing configurations,
ensuring synchronization between the software logic and the LED patterns. Additionally, as the
complexity of dimming patterns increased, the code needed to be optimized to handle multiple
sequences simultaneously without causing delays or overlaps.

How did the actual outcomes compare to the expected results?


The actual outcomes closely aligned with our theoretical expectations. The PWM emulation via
bitwise operations accurately produced the desired dimming and brightening patterns. However, minor
discrepancies in LED brightness at specific duty cycles highlighted the significance of real-world
testing and adjustments.

What improvements can be made for future experiments?


For future iterations, integrating an interface that allows real-time adjustments to duty cycles could
prove beneficial. Additionally, exploring other dimming techniques, beyond PWM, would provide a
comprehensive understanding of LED controls. Implementing interrupt-based control instead of
continuous polling can also improve efficiency.

Did the analog and digital dimming techniques produce noticeably different results?
Indeed, while both methods achieved LED dimming, the digital method, particularly PWM, offered a
more controlled and consistent brightness level. Analog dimming, though simpler, occasionally
presented minor color shifts.

9
Conclusions
The primary objective was to gain a practical understanding of LED dimming techniques, with an
emphasis on the PWM method. It’s gratifying to state that this objective was predominantly achieved.
The meticulous design and its implementation resulted in an accurate portrayal of the principles of
LED dimming. The experiment did not merely stop at the theoretical understanding of PWM but
ventured into its real-world applications, thereby providing a holistic perspective on the subject.
Like all practical implementations, the experiment was not without its challenges. A significant
challenge faced was the synchronization between the software logic and the physical manifestation of
dimming on the LEDs. At certain duty cycles, the LEDs exhibited minor discrepancies in brightness
levels compared to the expected results. Furthermore, as we sought to demonstrate intricate dimming
patterns, the complexity of the code grew, leading to overlapping sequences or unintended delays.

To address the synchronization issue, we revisited our bitwise operations and the associated timers. A
fine-tuning of the duty cycle adjustments in the code brought about a more synchronized dimming
effect. As for the complexity of sequences, segmenting the code into modular functions, and
employing an iterative testing approach, provided clarity and allowed for easier debugging. The
introduction of interrupt-based control, instead of continuous polling, is also a prospective solution for
future endeavors. the experiment was a significant leap towards understanding LED dimming at its
core. While challenges were an integral part of the journey, they paved the way for innovative
solutions, underlining the essence of practical learning. The experiment reinforced the principle that
theoretical knowledge, when complemented with hands-on experience, leads to a comprehensive
understanding of any subject matter.

10
11

You might also like