Lesson 29--Two Generative Styles of Microduino PWM & Waveform Measurement

From Microduino Wiki
Jump to: navigation, search

Two Generative Styles of Microduino PWM & Waveform Measurement


By this course, we can measure the waveform of Microduino PWM output port. By contrast of PWM output by analogWrite() and the PWM output waveform operated by the register, you'll be able to have an overall understanding of how Microduino outputs PWM.


Microduino-Core is the 8-bit single-chip development core board with Atmel ATmega328P as the core, which is an open and Arduino Uno compatible controller.

Microduino-USBTTL is the program download module, capable of connecting with Microduino-Core or Microduino-Core+ and making the modules communicate with the computer. It uses MicUSB as the download interface, which is also the smallest part of Microduino. Microduino is just as small as a quarter. Besides, the download cable is just like the USB cable of most smart phones, easy to use.

  • Other Equipment
Related hardware Number Function
20M oscillograph One For measuring Microduino PWM output waveform.


Oscillograph is a widely used electronic measurement device. It can turn the invisible electrical signal to visible image, easy for people to study various electrical phenomenon change. The X-axis of oscillograph represents time and the Y-axis stands for amplitude. The oscillograph image display in this example adopts CRT, which is old-fashioned. The new oscillograph adopts LCD as the display, which is much smaller than CRT in size and much more powerful in function.



Osc real conn.jpg

Principle Introduction: Microduino-Core adopts Arduino UNO compatible Atmega328p chip, which has much more improvement than MCS-51. Besides, the timer(atmega328p) is more flexible than that with MCS-51 chip, which has prescaler, high-output register added as well as the the ability of outputting PWM directly. By PWM, we can adjust the lightness of the LED as well as motor speed. In Arduino IDE, analogWrite() function can change duty ratio but can not change the frequency of PWM. In addition, the PWM frequency and duty ratio can both be adjustable.

Measuring analogWrite() waveform with Microduino-Core below The pin 3, 9, 10 and 11 have the same waveform basically and the oscillograph adjustment is 0.5ms/div 2V/div. Check below:

PIN 3 9 10 11 0.5ms-div 2V-div.jpg

The pin 5 and 6 share the same waveform basically and the oscillograph is 0.5ms/div 2V/div. Check below:

PIN 5 6 0.5ms-div 2V-div.jpg

Conclusion: The cycle of pin 3, 9, 10 and 11 is around 980Hz and the amplitude is below 5V. If changing the “127” in analogWrite(3,127), you can change the duty ratio based on the frequency.

Below is the PWM waveform image. Choose10us/div,2V/div as the oscillograph stall and D10 as the pin output.

PIN 10 10us-div 2V-div D=0.25.jpg

Another OCR1A=400; OCR1B=200;Measured drawing(10us/div,2V/div)

PIN 10 10us-div 2V-div D=0.5.jpg

Conclusion: By changing OCR1A, you can easily change the frequency and the cycle of PWM. However, by changing OCR1B, you can easily change the duty ratio under the set OCR1A cycle.


  • Adopt analogWrite():
void setup()
void loop()
  • Change register mode:
void setup()
void loop()
  TCCR1A =_BV(COM1A0) | _BV(COM1B1) | _BV(WGM11) | _BV(WGM10);   
  TCCR1B =_BV(WGM12) |_BV(WGM13) | _BV(CS10);   	
  // _BV(COM1A0) equal  COM1A0=1,the bit COM1A0 is in the TCCR1A register byte。
  //COM1A0=1; COM1B1=1;enable and choose  the  normal  output,not invert output 。
  // WGM11=1; WGM10=1; WGM12=1; WGM13=1; choose the fast PWM,the top value is OCR1A。OC1B (D10)is the only output pin
  // CS10=1; no prescaling 
  OCR1A=400;//change this value can set the frequency   F=16000000/400=40KHz
  OCR1B=100;//change this value can set the duty cycle   Duty=OCR1B/OCR1A


  • Copy the program to Arduino IDE.
  • Compile the program and choose the right board and the corresponding serial port.
  • Click Upload and observe the waveform and watch the waveform after upload.


The course introduces the output of Microduino PWM. By operating the register, you can adjust PWM waveform cycle and duty ratio. Since changing the related register value of the timer may affect some packed functions of Arduino, the change of the related register value of timer T1 doesn't change the one-second delay time. In the practical application circuit, such as switching power supply circuit, it may need a high-frequency PWM waveform to operate the related register of PWM as required.