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

Project 1.00 Blink

In this project, you’ll learn how to blink an LED!

Project 1.01 Blink x2

In this project, you’ll learn how to blink more than one LED!

Bri Wagner

Education Solution Specialist

Bri Wagner brings a bachelor’s degree in Elementary Education and nine years of classroom teaching experience to her role at 1st Maker Space. A former Teacher of the Year Award recipient, Bri is passionate about creating engaging, hands-on learning experiences that inspire student success. She understands the impact that innovative instruction and meaningful classroom resources can have on student achievement and is dedicated to partnering with educators to bring high-quality STEM solutions into classrooms. Bri is committed to empowering teachers and helping students develop the creativity, confidence, and problem-solving skills they need to thrive.