Digital Arduino Projects with TinkerCAD

Exercise One: State Change Detector (Button Counter)

Background Knowledge:

What you'll need:

1 - 9 Volt Battery

1 - 9 Volt Battery Harness

1 - Breadboard

1 - Arduino UNO

1 - N.O. Button

1 - Jumper Wire Set

State change detection is a technique used in Arduino programming to identify when a digital input (like a button press) changes from one state to another. In this lesson, we'll learn how to implement state change detection using a button. We will do that by having the Arduino keep track of how many times we click the button.

Steps

 2. Write the Arduino Sketch

int Click = 0;

int Counter = 0;


void setup()

{

  pinMode(2, INPUT);

  Serial.begin(9600);


  Click = 0;

  Counter = 0;

}

void loop()

{

  Click = digitalRead(2);

  if (Click == HIGH) {

    Counter = (Counter + Click);

    Serial.print("Number of Clicks: ");

    Serial.println(Counter);

    delay(150); // Wait for 150 millisecond(s)

  }

}

By opening and printing to the Serial Monitor, the Arduino and IDE is able to show you how many clicks the button has received by counting the number of state changes the button has had between 0 and 1, or HIGH and LOW. Because this state change was assigned to a variable, the variable keeps track of the number of times clicked and adds one each time there is another click. 

 3. Understand the Code

In the loop():

 4. Upload and Monitor

 5. Experiment and Observe

This lesson provides a foundational understanding of how to use state change detection, an important technique for projects where you need to respond to specific events triggered by digital inputs.

Questions: 

Exercise Two: RBG LED Rainbow

Background Knowledge:

What you'll need:

1 - 9 Volt Battery

1 - 9 Volt Battery Harness

1 - Breadboard

1 - Arduino UNO

1 - 1/4 Watt 470 Ohm THR

1 - RBG LED

1 - Jumper Wire Set

In this project, we'll use an RGB (Red, Green, Blue) LED to cycle through colors of the rainbow. This involves smoothly transitioning between different combinations of red, green, and blue to create a dynamic, colorful effect.

Steps

 2. Write the Arduino Sketch

void setup()

{

  pinMode(3, OUTPUT);

  pinMode(5, OUTPUT);

  pinMode(6, OUTPUT);

}

void loop()

{

  analogWrite(3, 255);

  analogWrite(5, 0);

  analogWrite(6, 0);

  delay(1000); // Wait for 1000 millisecond(s)

  analogWrite(3, 255);

  analogWrite(5, 153);

  analogWrite(6, 0);

  delay(1000); // Wait for 1000 millisecond(s)

}

 3. Understand the Code

In the loop():.

 4. Upload and Monitor

 5. Experiment and Observe

This lesson provides a foundational understanding of how to use state change detection, an important technique for projects where you need to respond to specific events triggered by digital inputs.

Questions: 

Exercise Three: RBG LED Controller

Background Knowledge:

What you'll need:

1 - 9 Volt Battery

1 - 9 Volt Battery Harness

1 - Breadboard

1 - Arduino UNO

1 - RBG LED

3 - 100K Potentiometer

1 - Jumper Wire Set

Creating a basic setup where a potentiometer controls an RGB LED on Arduino involves connecting the components and writing a simple sketch. This project showcases the fundamental principle of using analog input to control output intensity. Here's a step-by-step guide:

Steps

 2. Write the Arduino Sketch

int Red = 0;

int Green = 0;

int Blue = 0;


void setup()

{

  pinMode(A0, INPUT);

  pinMode(A1, INPUT);

  pinMode(A2, INPUT);

  pinMode(3, OUTPUT);

  pinMode(5, OUTPUT);

  pinMode(6, OUTPUT);

}


void loop()

{

  Red = map(analogRead(A0), 0, 1023, 0, 255);

  Green = map(analogRead(A1), 0, 1023, 0, 255);

  Blue = map(analogRead(A2), 0, 1023, 0, 255);

  analogWrite(3, Red);

  analogWrite(5, Green);

  analogWrite(6, Blue);

  delay(10); // Delay a little bit to improve simulation performance

}

 3. Understand the Code

In the loop():.

 4. Upload and Monitor

 5. Experiment and Observe

This setup demonstrates the core principle of using analog input (from the potentiometer) to control an output (the RGB LED). It serves as a foundation for more complex projects involving analog sensors and digital outputs. Understanding this basic concept is crucial for creating dynamic and responsive electronic systems.

Questions: 

Exercise Four: Servo Sweep

Background Knowledge:

What you'll need:

1 - 9 Volt Battery

1 - 9 Volt Battery Harness

1 - Breadboard

1 - Arduino UNO

1 - 180 Servo

1 - Jumper Wire Set

In this project, we'll make a servo motor sweep back and forth across its range of motion. This is a great way to understand how to control a servo motor with an Arduino.

Steps

 2. Write the Arduino Sketch

#include <Servo.h>


int pos = 0;

Servo servo_9;


void setup()

{

  servo_9.attach(9, 500, 2500);

}


void loop()

{

  for (pos = 0; pos <= 180; pos += 1) {

    servo_9.write(pos);

    delay(15); // Wait for 15 millisecond(s)

  }

  for (pos = 180; pos >= 0; pos -= 1) {

    servo_9.write(pos);

    delay(15); // Wait for 15 millisecond(s)

  }

}

 3. Understand the Code

In the loop():

 4. Upload and Monitor

 5. Experiment and Observe

This project demonstrates how to control a servo motor with an Arduino. Servo motors are commonly used in various applications, such as robotics and automation. Understanding how to control them opens up a wide range of possibilities for creating moving and interactive projects.

Extra Experiment:

Using the Oscilloscope tool on TinkerCAD, attach positive to the signal pin of the servo and negative to ground. Set the Oscilloscope to 1m/s divisions and simulate. You will see the pulse width modulation change as the servo sweeps. This is the signal telling the servo what position to spin to- the width of this signal is set to degrees in the servo's internal control unit.

Questions: 

Exercise Five: Servo Controller

Background Knowledge:

What you'll need:

1 - 9 Volt Battery

1 - 9 Volt Battery Harness

1 - Breadboard

1 - Arduino UNO

1 - 180 Servo

1 - 100K Potentiometer

1 - Jumper Wire Set

In this project, we'll use a potentiometer to control the position of a servo motor. This allows you to manually set the angle of the servo using the potentiometer knob.

Steps

 2. Write the Arduino Sketch

#include <Servo.h>


int pos = 0;

Servo servo_9;


void setup()

{

  pinMode(A0, INPUT);

  servo_9.attach(9, 500, 2500);

}


void loop()

{

  pos = map(analogRead(A0), 0, 1023, 0, 180);

  servo_9.write(pos);

  delay(10); // Delay a little bit to improve simulation performance

}

 3. Understand the Code

In the loop():

 4. Upload and Monitor

 5. Experiment and Observe

This project demonstrates how to use a potentiometer as an input device to control the position of a servo motor. It's a useful skill for creating projects that require precise control over the position of a motor, such as robotic arms and other automated systems.

Questions: 

Exercise Six: Proximity Sensor

Background Knowledge:

What you'll need:

1 - 9 Volt Battery

1 - 9 Volt Battery Harness

1 - Breadboard

1 - Arduino UNO

1 - Ultra Sonic Proximity Sensor

1 - Piezo Buzzer

1 - Jumper Wire Set

In this project, we'll create a simple alarm system using a proximity sensor and a buzzer. The buzzer will sound when an object is detected within a certain range of the proximity sensor.

Steps

 2. Write the Arduino Sketch

int Inches = 0;

int Centimeters = 0;


long readUltrasonicDistance(int triggerPin, int echoPin)

{

  pinMode(triggerPin, OUTPUT);  // Clear the trigger

  digitalWrite(triggerPin, LOW);

  delayMicroseconds(2);

  // Sets the trigger pin to HIGH state for 10 microseconds

  digitalWrite(triggerPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(triggerPin, LOW);

  pinMode(echoPin, INPUT);

  // Reads the echo pin, and returns the sound wave travel time in microseconds

  return pulseIn(echoPin, HIGH);

}


void setup()

{

  pinMode(8, OUTPUT);

}


void loop()

{

  if (0.006783 * readUltrasonicDistance(3, 2) < 20) {

    tone(8, 523, 100); // play tone 60 (C5 = 523 Hz)

  }

  delay(10); // Delay a little bit to improve simulation performance

}

 3. Understand the Code:

The code uses a simple distance calculation based on the time it takes for the ultrasonic pulse to return. If the calculated distance is less than 20 inches (adjust as needed), the buzzer is activated; otherwise, it's deactivated.

4. Upload and Monitor:

5. Test the Alarm:

Questions: 

Exercise Seven: Motor Controller

Background Knowledge:

What you'll need:

1 - 9 Volt Battery

1 - 9 Volt Battery Harness

1 - Breadboard

1 - Arduino UNO

1 - 6-12V DC Motor

1 - 1K 1/4 Watt THR

2 - 10K 1/4 Watt THR

2 - 3904 NPN Transistor

2 - 3906 PNP Transistor

1 - 100K Potentiometer

1 - Jumper Wire Set

Step into the world of precise motor control with the Arduino Motor Controller Circuit – a gateway to unleashing the full potential of your electric motors. This innovative circuit, built upon the versatile Arduino platform, empowers enthusiasts and engineers to regulate the speed and direction of motors with unparalleled ease. By seamlessly integrating sensors, feedback mechanisms, and programmable logic, the Arduino Motor Controller Circuit opens up a realm of possibilities for automation, robotics, and DIY projects. Whether you're a hobbyist or a seasoned developer, this circuit provides a user-friendly interface to harness the dynamic capabilities of electric motors, transforming your ideas into seamless, controlled motion

Motor Set-Up One

Steps

 2. Write the Arduino Sketch

The speed regulation of a DC motor is intricately linked to the voltage it receives. In order to optimize a motor's performance and ensure it operates at its maximum power, maintaining high levels of both voltage and current is crucial. The Arduino employs a technique known as pulse width modulation (PWM) to achieve precise control over a motor's speed. Instead of directly adjusting the voltage supplied to the motor, PWM rapidly switches the motor between full power and no power. By modulating the duration of the 'on' and 'off' states in a cycle, the Arduino effectively controls the average power delivered to the motor. When the motor is on for a greater portion of the cycle, it spins faster, and when it's off for a longer duration, the speed decreases. This dynamic manipulation of the power ratio enables the Arduino to finely adjust the speed of the motor without compromising its overall power output.


When writing this sketch, just output a PWM pin to between 0-255.

 3. Understand the Code:

4. Upload and Monitor:

5. Test the Motor Controller:

Motor Set-Up Two

While it is possible to control a DC motor directly from an Arduino, there are certain scenarios where doing so might not be the best approach. Here are some reasons why you might choose not to control a DC motor directly from an Arduino:

For these reasons, it's often recommended to use external motor driver modules or controllers designed for the specific requirements of DC motors. These modules can handle higher currents, provide proper isolation, and include features like built-in protection circuits, making them more suitable for motor control applications.

Steps

Why a Capacitor?

Using a capacitor in parallel with a DC motor is a common practice in some applications, but it's not a strict requirement for all setups. The decision to include a capacitor depends on the specific characteristics of the motor, the power supply, and the requirements of the circuit. Here are a few considerations for using a capacitor in parallel with a DC motor:

However, not all DC motor setups require a capacitor, and in some cases, it might not be necessary or could even be detrimental. It's essential to consider the specific characteristics of your motor, the power supply, and the requirements of your application before deciding to include a capacitor. If in doubt, referring to the motor manufacturer's recommendations or consulting relevant documentation is advisable.

Why a Diode?

Placing a diode in parallel with a DC motor, commonly referred to as a "freewheeling" or "flyback" diode, is a common practice in electronic circuits. The primary purpose of this diode is to provide a path for the inductive energy stored in the motor's coil to dissipate safely when the power to the motor is suddenly cut off. When a DC motor is in operation, it builds up magnetic energy in its coil. If the power to the motor is suddenly switched off, this energy needs a path to dissipate. Without a freewheeling diode, the collapsing magnetic field in the motor coil can create a voltage spike, known as a back electromotive force (EMF), that can potentially damage other components in the circuit.

The freewheeling diode provides a low-resistance path for the inductive energy to circulate back into the circuit when the power is turned off. By doing so, it prevents voltage spikes and protects other components from potential damage. The diode is typically connected in reverse bias, meaning its cathode is connected to the positive side of the motor and its anode to the negative side. This configuration ensures that when the power is turned off, the diode becomes forward-biased, allowing the inductive energy to circulate through the diode and back into the circuit.

In summary, the freewheeling diode is a protective measure to prevent voltage spikes and potential damage to the circuit when the power to a DC motor is abruptly interrupted.

 2. Write the Arduino Sketch

 3. Understand the Code:

4. Upload and Monitor:

5. Test the Motor Controller:

Motor Set-Up Three

The final set-up that we will cover is using an H-bridge. An H-bridge is a circuit configuration commonly used for controlling the direction and speed of DC motors. It consists of four switches arranged in the shape of an "H," hence the name. The H-bridge allows the motor to be driven in both forward and reverse directions and enables speed control through Pulse Width Modulation (PWM). Here's an explanation of how an H-bridge works to control motors:

Components of an H-Bridge:

Operation:

Forward Rotation: To make the motor rotate in the forward direction, switches Q1 and Q4 are turned on, while Q2 and Q3 are turned off. This creates a complete circuit, allowing current to flow from the power supply, through the motor, and back to the power supply. The motor spins in the desired direction.

Reverse Rotation: To reverse the motor's rotation, switches Q2 and Q3 are turned on, while Q1 and Q4 are turned off. This reverses the direction of the current flow through the motor, causing it to rotate in the opposite direction.

Braking: By turning off all switches, the motor is effectively disconnected from the power supply. However, the back electromotive force (EMF) generated by the motor could damage the circuit. To address this, a technique called "regenerative braking" involves turning on both switches on one side of the H-bridge to create a short circuit, allowing the motor to dissipate its energy safely.

Speed Control with PWM: To control the motor's speed, Pulse Width Modulation (PWM) is applied to the switches. By rapidly switching the switches on and off with varying duty cycles, the effective voltage applied to the motor is adjusted, regulating its speed.

Using an H-bridge provides a flexible and efficient way to control DC motors, making it a fundamental component in various applications such as robotics, automation, and electric vehicles.

Steps

 2. Write the Arduino Sketch

 3. Understand the Code:

    analogWrite(3, 0);

    analogWrite(5, map(analogRead(A0), 0, 1023, 0, 255));

4. Upload and Monitor:

5. Test the Motor Controller:

Questions: 

Exercise Seven: LCD Display (Coming Soon)

Background Knowledge:

What you'll need:

1 - 9 Volt Battery

1 - 9 Volt Battery Harness

1 - Breadboard

1 - Arduino UNO

1 - 180 Servo

1 - 100K Potentiometer

1 - Jumper Wire Set