Source Code for Arduino Functions

I have been trying to find the source code for the functions analogWrite() etc. I looked how to do it on the forum but I can't find the hardware or cores folder in my C drive. What should I do? I need this for a project.

Install cygwin for some decent search tools.

First, it's very important to understand that each boards package provides its own version of the core library that implements the standard Arduino API functions like analogWrite(). So if you do a search of your hard drive and fine a definition of that function, it may not necessarily be the one used by the Arduino board you are using.

Because the API is standardized and is identical, or at least compatible, no matter which board you are using, it's easy to think there is only a single implementation. However, the implementation of a hardware-specific function like analogWrite() can be completely different from one microcontroller architecture to the next.

In addition, in the case of the Arduino AVR Boards platform of the popular Uno, Nano, Leonardo, Mega, etc. boards, you may have two copies on your computer, only one of which is in use.


The easiest way to find the active hardware package location is as follows:

  • Select a board from the hardware package you want to find from the Tools > Board menu
  • File > Examples > SPI > BarometricPressureSensor (or any other SPI example sketch)
  • Sketch > Show Sketch Folder
  • Move up folder levels until you reach the one that contains boards.txt

You should now find the definition of analogWrite() in cores/arduino/wiring_analog.c (though it's possible it could be in a different file, that filename is just the common convention).

pratap9: I have been trying to find the source code for the functions analogWrite() etc. I looked how to do it on the forum but I can't find the hardware or cores folder in my C drive. What should I do? I need this for a project.

Look online https://github.com/arduino/Arduino/tree/master/hardware

Look here for the source codes of analogWrite() function.

GolamMostafa: Look here for the source codes of analogWrite() function.

But ONLY for the Arduino AVR Boards platform.

Take a look at how different it is for the Arduino SAMD Boards platform of the MKR, Zero, and Nano 33 IoT boards: https://github.com/arduino/ArduinoCore-samd/blob/1.8.7/cores/arduino/wiring_analog.c#L190-L317

pert: But ONLY for the Arduino AVR Boards platform.

Yes!