Project 1.03 Analog Write

In this project, you will learn about a new function called analogWrite!

In this project you will learn about a new function called analogWrite. This function is useful for setting the brightness of LEDs. It can also be used to set the speed of motors, servo motor position, generating audio tones, and more.

The MC Trainer can only produce values of 0v and 5v but what happens when you need 2.5v? Simply put, the MC trainer cannot produce 2.5v. Instead, what it can do is switch a pin between 0v and 5v very quickly and produce an average voltage of 2.5v. This process is known as PWM.

It is important to note that this can only be done with certain pins. In our case, LED1 and LED2 can be used to do PWM.

This also introduces the Idea of duty cycle. Duty cycle is the ratio of how long a signal is on VS how long a signal is off. In this case the duty cycle would be 50%.

Project Code:

				
					/////////////////////////////////////////////////
// 1.03 - Analog Write

byte LED1 = 13;

void setup() {
  pinMode(LED1, OUTPUT);
  analogWrite(LED1, 127); 
}

void loop() {

}
/////////////////////////////////////////////////
				
			

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

The analogWrite function takes an 8-bit value between 0 – 255. 0 represents 0v and 255 represents 5v. The average voltage produced by that pin can be calculated with the equation: (analogValue * 5) / 255, with analogValue being the 8-bit number passed to the analogWrite function. For example, (127 * 5) / 255 = 2.49V. So, 127 will produce 2.49 volts.

				
					analogWrite(LED1, 127); 
				
			

When you run the code, the LED will be at half brightness.

Use the simulator below to explore different duty cycles!

O-Scope
47%
5

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.