Notes Sketching 1
Notes Sketching 1
An Arduino sketch is the term used for a program written for Arduino boards. It consists of specific
sections that help control the behavior of the microcontroller.
a. Setup Function (setup())
• Purpose: Initializes settings, runs once when the Arduino is powered on or reset.
• Syntax:
• Common Uses:
o Setting pin modes (pinMode())
o Starting serial communication (Serial.begin())
o Initializing libraries
b. Loop Function (loop())
• Purpose: Contains the code that runs repeatedly after setup() finishes.
• Syntax:
• Common Uses:
o Reading sensor data
o Controlling outputs (LEDs, motors, etc.)
o Monitoring inputs continuously
c. Pin Declaration
• Purpose: Defines which pins will be used as input or output.
• Syntax:
• Common Uses:
o Assigning descriptive names to pin numbers for clarity
o Using constants (const int buttonPin = 2;)
4. Test Frequently:
Upload small parts of your code to the Arduino board regularly instead of writing everything at once.
This helps catch errors early and makes debugging easier.
5. Use Constants for Fixed Values:
Define constants for values like pin numbers to avoid accidental changes and make updates easier.
Use const or #define.
Example:
Don'ts:
1. Avoid Hardcoding Values:
Instead of writing raw numbers directly in your code (like digitalWrite(13, HIGH) everywhere), assign
them to variables or constants. This makes changes easier and prevents mistakes.
Questions:
1. Which sketch is correct?
2. Identify the errors in the incorrect sketch.
3. Explain how to fix the errors.