Embedded c program for LED

Embedded hash
4 min readNov 15, 2023

--

Introduction
Welcome back! Now that you’ve successfully made an LED blink, let’s dive deeper into the magic of Embedded C and explore additional concepts. In this extended guide, we’ll enhance our LED project, introducing new elements and functionality. Get ready to unravel more of the embedded systems world with simple explanations.

Introducing Variables: Adding Flexibility to Your LED
In our previous code, the LED blinked on and off like clockwork. Now, let’s introduce a variable to add some flexibility. A variable is like a storage box where we can keep a changing value.

Updated Code:

int led = 13; // Pin number for the LED
int delayTime = 500; // Time in milliseconds for the LED delay
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(delayTime);
digitalWrite(led, LOW);
delay(delayTime);
}

What’s New?
int delayTime = 500;: We introduced a new variable, ‘delayTime,’ to control the delay duration between LED states. The value 500 means 500 milliseconds (or half a second).
Now, by changing the ‘delayTime’ value, you can control how fast or slow your LED blinks without rewriting the entire code.

Conditional Statements: Making Decisions in Code
Let’s make our LED smarter by using conditional statements. These are like decision-making rules for your code.

Updated Code:

int led = 13;
int delayTime = 500;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(delayTime);
if (delayTime == 500) {
delayTime = 1000; // If the delay is 500ms, change it to 1000ms
} else {
delayTime = 500; // If the delay is not 500ms, change it to 500ms
}
digitalWrite(led, LOW);
delay(delayTime);
}

What’s New?
if (delayTime == 500): This line checks if ‘delayTime’ is equal to 500. If true, it changes ‘delayTime’ to 1000; otherwise, it sets it back to 500.
Now, your LED will blink at 500ms for a moment, then switch to 1000ms, creating a dynamic pattern.

Serial Communication: Talking to Your Computer
Let’s add a feature that communicates with your computer. This is handy for debugging and understanding what your program is doing.

Updated Code:

int led = 13;
int delayTime = 500;

void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
digitalWrite(led, HIGH);
delay(delayTime);

if (delayTime == 500) {
delayTime = 1000;
Serial.println(“Blinking Slowly!”);
} else {
delayTime = 500;
Serial.println(“Blinking Quickly!”);
}

digitalWrite(led, LOW);
delay(delayTime);
}

What’s New?
Serial.begin(9600): This line initiates serial communication with your computer at a speed of 9600 bits per second (bps).

Serial.println(“…”): These lines send messages to your computer. Check the Serial Monitor in the Arduino IDE to see these messages.

Now, your LED not only blinks but also tells your computer whether it’s blinking slowly or quickly.

Analog Control: Playing with Brightness
Let’s explore analog control to make the LED shine at different brightness levels, rather than just turning on and off.

Updated Code:
c
Copy code
int led = 9; // Use a PWM (Pulse Width Modulation) pin for analog control
int brightness = 0; // Start with the LED off

void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}

void loop() {
analogWrite(led, brightness); // Set the LED brightness

brightness = brightness + 5; // Increase brightness gradually

if (brightness > 255) {
brightness = 0; // Reset brightness to 0 when it reaches maximum
}

delay(30); // Small delay for a smoother transition
}
What’s New?
int led = 9: We’re using a PWM pin (Pin 9) for analog control. PWM allows us to vary the LED’s brightness by adjusting the amount of time it’s on during each cycle.

analogWrite(led, brightness): Instead of just turning the LED on or off, we’re now adjusting its brightness using ‘analogWrite.’ The brightness value ranges from 0 (off) to 255 (maximum brightness).

Now, your LED will gradually get brighter and reset, creating a pulsating effect.

Wrapping Up Your Enhanced LED Project
Congratulations! You’ve gone beyond the basics of LED blinking and explored new concepts like variables, conditional statements, serial communication, and analog control. Each addition to your code brings more versatility and understanding of Embedded C.

Remember, the essence of learning lies in experimentation. Modify the code, add your features, and see how it changes the LED’s behavior. This journey is not just about making an LED blink; it’s about unlocking the potential of Embedded C and gaining hands-on experience in the fascinating world of embedded systems. Happy coding!

If you want to learn Embedded C Come and join us at Embedded Hash

If you’re excited to learn Embedded C, join us at Embedded Hash! We make learning easy, and we even help you find a job afterward. Come be a part of our friendly community where we teach you everything step by step.

No need to worry about finding a job — we’ve got your back! Let’s make learning Embedded C training in Hyderabad fun and rewarding together. Join us at Embedded Hash for a great learning experience and a chance to kickstart your career with job assistance!

--

--