Skip to content

Arduino Notes

Microchip/Atmel ATmega328P is the micro controller chip on the arduino board.

Below are Taken while learning from Paul Whorter Tutorials and playing with Arduino. Then towards the bottom starting at # Define, I just kept adding nuggets as I found them.

analogWrite - value between 0 and 255

digitalWrite - its on or off, its HIGH or LOW, 0 or 1

Coutning in Binary Numbers 1 2 4 8 16 32 64 128 256 8 bits = byte

0 takes one of the bits… so you stop at 255 for 256 slots

Volts Pin Value
0 volts 0
Volts 255

Standard Digital Pins: Pins 0-13 can all do digitalWrite() and digitalRead()

Special Functions: - PWM Pins (~):Pins with a ~ symbol next to them: 3, 5, 6, 9, 10, 11 - These can do analogWrite() for PWM (Pulse Width Modulation) - 3, 5, 6, 9, 10, 11 - can do analogWrite() for brightness control

Quick reference: - A0-A5 (no ~) → analogRead() - read sensors (0-1023) - So if you're connecting a potentiometer or light sensor, use A0-A5.

  • 3, 5, 6, 9, 10, 11 (with ~) → analogWrite() - control brightness (0-255)
    • If you're controlling LED brightness or motor speed, use the ~ pins.

Serial Communication: - 0 (RX) - receives serial data - 1 (TX) - transmits serial data

Avoid using these if you need Serial Monitor - SPI Communication: - 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK) - I2C Communication: - A4 (SDA), A5 (SCL) - also analog pins

Built-in LED: Pin 13 - has an LED soldered on the board

digitalWrite(pin, HIGH/LOW);

PWM (Pulse Width Modulation) - oscilloscope showing us the voltage coming out of the arduino. When you are at 0 the oscilloscope is at zero and at 255, the oscilloscope is at almost 5. But when you set the brightness to 127 in the middle, it switches back and forth every 2 milliseconds from almost 5 to zero. It switches back and forth to average the value that you set as the PWM value. PWM switching on oscillator whorter Oscillations If you really want a smooth voltage, then add a capacitor. Make sure that you're reading the voltage using the Arduino using the A0-A5 pins

A pins read a value between 0 - 1023, so we need to scale voltage reading to get actual voltage

Actual Voltage Flowing in circuit at that point = (Voltage Reading *5.0V)/1023.0 Use 5.0 and 1023.0 so that you avoid floating point errors.

IF statements

Conditionals

!=  does not equal  
==  equals  
&&  and
||      or 
< > <= >=
if (V2>4.0) {
  digitalWrite(redPin, HIGH);
}

FOR LOOPS

for (j=1; j<=yellowBlink; j=j+1) {
  digitalWrite(greenPin, HIGH);
  delay(yellowTime);
  digitalWrite(greenPin, LOW);
  delay(yellowTime);

More work on FOR LOOPS = Tutorial 16

for (j=1; conditional; j=j+1)

WHILE LOOPS

Good for project based things - tutorial 17

while (potVal>1000){
  digitalWrite(redPin, HIGH);
  potVal=analogRead(potPin);
  Serial.println(potVal);
  delay(delayTime);
  //j=j+1;
  }
digitalWrite(redPin,LOW);
Serial.println();

Serial Monitor
Serial.begin(9600);
Serial.println(msg);
while (Serial.available()==0){
  //do absolutely noth - this is to hang your program until something is entered
}
//myNumber=Serial.parseInt();
numBlinks=Serial.parseInt();
//Serial.println(myNumber);
}
radius=Serial.parseFloat();
myName=Serial.readString();

MIXING COLORS USING analogWrite and analogWrite

if (myColor=="off"){
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, LOW);
}
if (myColor=="yellow"){
  analogWrite(redPin, 255);
  analogWrite(greenPin, 100);
  analogWrite(bluePin, 0);
}

Buzzer

Active Buzzer - has sticker.. And is easiest to use because you just hook electricity up to it.. Note: it does have polarity.. This one has sticker. Doesnt need microseconds.

Passive Buzzer (has a green background in this case) Requires a an AC current, but cheaper - just need to write a digital write high and then low, and modulating that can make different tones and songs. Need to use delayMicroseconds(); to get up to the higher sounds 100-1000+

Making a program with potentiometer to go to tone of 60 microseconds - then (0,60) to (1023, 10000)

Tone = (9940/1023)*(potVal)+60

FOR BUZZER - you have to modulate - on/off on/off to make different sounds by changing the delay time

for (j=1; j<=100; j=j+1){
  digitalWrite(buzzPin, HIGH);
  delay (dt1);
  digitalWrite(buzzPin, LOW);
  delay(dt1);
Push Button - Pull Up Resistor
digitalRead - this reads value 0 or 1
buttonNew=digitalRead(buttonPin);
Curved edges of the pin go in the same column

SERVO

hiTech - servo can also be powered from the arduino board

  • NOTE: must know what is the output capability of the pin you are using to make sure you can power the servo from that pin, if not you need to add an external power. You can still control from the arduino but you may need an external power supply

Redwire to red center pin of servo then to 5V of arduino Black arduino ground to servo brown ground

To include the servo library Before all

#include <Servo.h>
Void Set Up
myServo.attach(servoPin);
Void Loop
myServo.write(angle);

When using a light sensor, the values for sensing lights off was 220 lights on at 530

Understanding Hexadecimal Numbers and Why They Are Important was Arduino Tutorial 41: 0-F (base 16). Can store many more numbes more compactly. They also relate to the binary numbers very easily. hex to binary

# Define

#define DIR_PIN 2
#define STEP_PIN 3
int buttonPin1=4;
int buttonPin2=9;
int buttonVal1;

Claude Prompt: what is #define

define is a preprocessor directive that creates a text substitution before your code compiles. It's like a "find and replace" that happens automatically.

The #define doesn't use memory. Its mainly used for pin definitions and constants.