• Arduino Voltage and Frequency Simulation | Tinkercad

QC

Arduino Voltage and Frequency Simulation | Tinkercad

Learn how voltage controls frequency with this interactive Arduino simulation in Tinkercad, even if you don't have the physical components.

Devices and components

16x2 LCD screen with I²C interface

Arduino Uno Rev3

Breadboard - 830 contacts

Piezoelectric buzzer

10kOhm potentiometer

Oscillator IC, voltage controlled

7-segment LED display, InfoVue

Jumper wires (generic)

Resistance 220 ohms

Software and tools

Arduino IDE

Autodesktinkercad.com

Project description

Background:

Why Tinkercad?

Accessibility: Learners can access the simulation from anywhere with an Internet connection, eliminating barriers of cost and availability of physical components.

Safety: Virtual simulations provide a risk-free environment for experimentation, especially when it comes to electricity.

Conceptual understanding: Simulations can help focus on fundamental concepts and relationships without the distractions of real-world complexities.

The voltage-frequency relationship:

Pulse: A sudden change in voltage or current, such as a single beat.

Wave: A smooth, continuous change, like a sound wave or radio wave.

Think about it like this:

Higher Voltage: More “pressure” on the oscillator, causing it to generate the signal faster (higher frequency).

Lower Voltage: Less “pressure”, resulting in slower signal generation (lower frequency).

Audio synthesis: Synthesizers use oscillators to create different musical notes by controlling the frequency of the oscillator with voltage.

Radio communication: Radio transmitters use oscillators to generate radio waves at specific frequencies, and voltage control allows different channels to be tuned.

Motor control: The speed of some motors can be controlled by varying the frequency of the electrical signal, which is often related to voltage.

Components:

Arduino Uno board (simulated in Tinkercad)

Breadboard (simulated in Tinkercad)

Oscillator (simulated in Tinkercad)

10k ohm potentiometer (simulated in Tinkercad)

Two-digit common anode 7-segment display (simulated in Tinkercad)

16x2 LCD screen (simulated in Tinkercad)

Piezoelectric buzzer

Circuit diagram:

Relationships:

Connect segment A (a) to digital pin 2.

Connect segment B (b) to digital pin 3.

Connect segment C (c) to digital pin 4.

Connect segment D (d) to digital pin 5.

Connect segment E (e) to digital pin 6.

Connect the F segment (f) to digital pin 7.

Connect segment G (g) to digital pin 8.

Connect the decimal point (DP) to digital pin 9.

Connect common 1 to the positive (+) rail of the breadboard.

Connect common 2 to the positive (+) rail of the breadboard.

Add a 220 ohm resistor in series with each segment pin (between the display and the Arduino pin).

Connect one branch of the oscillator to digital pin 12.

Connect the other leg of the oscillator to GND.

Connect an outside leg to 5V.

Connect the other outer leg to GND.

Connect the middle foot (wiper) to analog pin A0.

Connect VCC to the positive (+) rail of the breadboard.

Connect GND to the negative (-) rail of the breadboard.

Connect SDA to A4.

Connect SCL to A5.

Connect one branch of the piezoelectric buzzer to digital pin 11.

Connect the other branch of the piezoelectric buzzer to GND.

Code:

/*

Sketch Title: Illustrating the Relationship Between Voltage and Frequency

Description: This sketch demonstrates how changing voltage affects

the frequency of an oscillator, using a potentiometer,

a 7-segment display, an LCD screen, and a piezo speaker in Tinkercad.

This example code is in the public domain.

Modified 29 Nov 2024

by Google Gemini Advanced and VideotronicMaker (Tishin)

*/

#include <LiquidCrystal_I2C.h>

// Set the LCD address for a 16 chars and 2 line display

LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 is the most common I2C address

// Define 7-segment display pins

const int segA = 2;

const int segB = 3;

const int segC = 4;

const int segD = 5;

const int segE = 6;

const int segF = 7;

const int segG = 8;

const int segDP = 9;

// Define potentiometer pin

const int potPin = A0;

// Define oscillator pin

const int oscillatorPin = 12;

// Define piezo speaker pin

const int speakerPin = 11; // Using digital pin 11 for the speaker

void setup() {

// Initialize the LCD

lcd.init();

lcd.backlight(); // Turn on the backlight

// Set 7-segment display pins as outputs

for (int i = 2; i <= 9; i++) {

pinMode(i, OUTPUT);

}

// Set oscillator and speaker pins as outputs

pinMode(oscillatorPin, OUTPUT);

pinMode(speakerPin, OUTPUT);

}

void loop() {

// Read potentiometer value

int potValue = analogRead(potPin);

// Map potentiometer value to frequency range (adjust as needed)

int frequency = map(potValue, 0, 1023, 100, 1000);

// Generate frequency on oscillator and speaker pins

tone(oscillatorPin, frequency);

tone(speakerPin, frequency);

// Display frequency on 7-segment display

displayFrequencyOnSevenSegment(frequency);

// Display frequency and pot value on LCD

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Freq: ");

lcd.print(frequency);

lcd.print(" Hz");

lcd.setCursor(0, 1);

lcd.print("Pot: ");

lcd.print(potValue);

delay(100); // Reduced delay for smoother sound

}

// Function to display frequency on 7-segment display

void displayFrequencyOnSevenSegment(int frequency) {

int hundredsDigit = frequency / 100;

int tensDigit = (frequency / 10) % 10;

// Display hundreds digit on the first 7-segment display

digitalWrite(10, HIGH); // Enable the first digit (common anode)

digitalWrite(11, LOW); // Disable the second digit

displayDigit(hundredsDigit);

delay(5); // Delay to prevent flickering

// Display tens digit on the second 7-segment display

digitalWrite(10, LOW); // Disable the first digit

digitalWrite(11, HIGH); // Enable the second digit (common anode)

displayDigit(tensDigit);

delay(5); // Delay to prevent flickering

}

void displayDigit(int digit) {

// This function sets the appropriate segment pins LOW or HIGH

// to display the given digit on a common anode display.

switch (digit) {

case 0:

digitalWrite(segA, LOW);

digitalWrite(segB, LOW);

digitalWrite(segC, LOW);

digitalWrite(segD, LOW);

digitalWrite(segE, LOW);

digitalWrite(segF, LOW);

digitalWrite(segG, HIGH);

digitalWrite(segDP, HIGH); // Decimal point off

break;

case 1:

digitalWrite(segA, HIGH);

digitalWrite(segB, LOW);

digitalWrite(segC, LOW);

digitalWrite(segD, HIGH);

digitalWrite(segE, HIGH);

digitalWrite(segF, HIGH);

digitalWrite(segG, HIGH);

digitalWrite(segDP, HIGH); // Decimal point off

break;

case 2:

digitalWrite(segA, LOW);

digitalWrite(segB, LOW);

digitalWrite(segC, HIGH);

digitalWrite(segD, LOW);

digitalWrite(segE, LOW);

digitalWrite(segF, HIGH);

digitalWrite(segG, LOW);

digitalWrite(segDP, HIGH); // Decimal point off

break;

case 3:

digitalWrite(segA, LOW);

digitalWrite(segB, LOW);

digitalWrite(segC, LOW);

digitalWrite(segD, LOW);

digitalWrite(segE, HIGH);

digitalWrite(segF, HIGH);

digitalWrite(segG, LOW);

digitalWrite(segDP, HIGH); // Decimal point off

break;

case 4:

digitalWrite(segA, HIGH);

digitalWrite(segB, LOW);

digitalWrite(segC, LOW);

digitalWrite(segD, HIGH);

digitalWrite(segE, HIGH);

digitalWrite(segF, LOW);

digitalWrite(segG, LOW);

digitalWrite(segDP, HIGH); // Decimal point off

break;

case 5:

digitalWrite(segA, LOW);

digitalWrite(segB, HIGH);

digitalWrite(segC, LOW);

digitalWrite(segD, LOW);

digitalWrite(segE, HIGH);

digitalWrite(segF, LOW);

digitalWrite(segG, LOW);

digitalWrite(segDP, HIGH); // Decimal point off

break;

case 6:

digitalWrite(segA, LOW);

digitalWrite(segB, HIGH);

digitalWrite(segC, LOW);

digitalWrite(segD, LOW);

digitalWrite(segE, LOW);

digitalWrite(segF, LOW);

digitalWrite(segG, LOW);

digitalWrite(segDP, HIGH); // Decimal point off

break;

case 7:

digitalWrite(segA, LOW);

digitalWrite(segB, LOW);

digitalWrite(segC, LOW);

digitalWrite(segD, HIGH);

digitalWrite(segE, HIGH);

digitalWrite(segF, HIGH);

digitalWrite(segG, HIGH);

digitalWrite(segDP, HIGH); // Decimal point off

break;

case 8:

digitalWrite(segA, LOW);

digitalWrite(segB, LOW);

digitalWrite(segC, LOW);

digitalWrite(segD, LOW);

digitalWrite(segE, LOW);

digitalWrite(segF, LOW);

digitalWrite(segG, LOW);

digitalWrite(segDP, HIGH); // Decimal point off

break;

case 9:

digitalWrite(segA, LOW);

digitalWrite(segB, LOW);

digitalWrite(segC, LOW);

digitalWrite(segD, LOW);

digitalWrite(segE, HIGH);

digitalWrite(segF, LOW);

digitalWrite(segG, LOW);

digitalWrite(segDP, HIGH); // Decimal point off

break;

default:

// Turn off all segments for unknown digits

for (int i = 2; i <= 9; i++) {

digitalWrite(i, HIGH);

}

break;

}

}

Explanation:

Potentiometer Reading: The analogRead(potPin) function reads the voltage of the potentiometer connected to analog pin A0. This voltage corresponds to the position of the potentiometer, with the value 0 representing one extreme and 1023 representing the other.

Mapping voltage to frequency: The map(potValue, 0, 1023, 100, 1000) function converts the raw potentiometer value (0-1023) to a corresponding frequency value (100 Hz to 1000 Hz). This mapping ensures that as you turn the potentiometer, the frequency changes proportionally within the specified range.

Generating the oscillator signal: The tone function (oscillatorPin, frequency) generates a square wave with the calculated frequency on digital pin 12. This pin is connected to the oscillator in the Tinkercad simulation.

Display on 7-segment display: The function displayFrequencyOnSevenSegment (frequency) processes the frequency value. It extracts the hundreds and tens digits, then uses the displayDigit() function to activate the appropriate segments on the 7-segment display, showing the numerical value of the frequency.

LCD Display: The LCD displays both the calculated frequency and the raw potentiometer value. This provides a dual perspective on the relationship between potentiometer position, resulting voltage, and generated frequency.

Experiment and learn:

Change frequency range: Change the values of the map() function to experiment with different frequency ranges. Observe how this affects the output on the 7-segment display and LCD screen.

Listen to the frequency: Connect a virtual speaker or buzzer to digital pin 12 of your Tinkercad circuit. This will allow you to hear the frequency generated by the oscillator and how it changes as you adjust the potentiometer.

View the waveform: Use the Tinkercad oscilloscope to view the waveform generated by the oscillator. Connect the oscilloscope probes to digital pin 12 and GND. Observe how the waveform changes as you adjust the potentiometer.

Edit the code: Experiment with different delay values in the loop() function to control how often the displays are updated. You can also try displaying other information on the LCD, such as the period or duty cycle of the waveform.

Improved with audio feedback:

How it works:

The piezo speaker is connected to digital pin 11 of the Arduino Uno. This pin generates the sound waves based on the calculated frequency.

The code uses the tone function (speakerPin, frequency) to produce a square wave with the desired frequency, driving the piezo speaker.

As you turn the potentiometer and change the voltage, the frequency updates, resulting in an audible change in the speaker's pitch.

Advantages of Piezo Speaker:

Intuitive Understanding: Hearing the pitch change in real-time provides a more intuitive grasp of how frequency varies with voltage.

Real-world connection: The speaker connects the simulation to real-world applications such as music synthesizers and audio systems.

Increased Engagement: The interactive audio element makes the learning process more engaging and enjoyable.

Smoothing for clarity:

Acknowledgments:

Illustrate the relationship between voltage and frequency

Relationship between voltage and frequency

1/*
2 Sketch Title: Illustrating the Relationship Between Voltage and Frequency
3
4 Description: This sketch demonstrates how changing voltage affects
5 the frequency of an oscillator, using a potentiometer,
6 a 7-segment display, an LCD screen, and a piezo speaker in Tinkercad.
7
8 This example code is in the public domain.
9
10 Modified 29 Nov 2024
11 by Google Gemini Advanced and VideotronicMaker (Tishin)
12*/
13
14#include <LiquidCrystal_I2C.h>
15
16// Set the LCD address for a 16 chars and 2 line display
17LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 is the most common I2C address
18
19// Define 7-segment display pins
20const int segA = 2;
21const int segB = 3;
22const int segC = 4;
23const int segD = 5;
24const int segE = 6;
25const int segF = 7;
26const int segG = 8;
27const int segDP = 9;
28
29// Define potentiometer pin
30const int potPin = A0;
31
32// Define oscillator pin
33const int oscillatorPin = 12;
34
35// Define piezo speaker pin
36const int speakerPin = 11; // Using digital pin 13 for the speaker
37
38
39void setup() {
40 // Initialize the LCD
41 lcd.init();
42 lcd.backlight(); // Turn on the backlight
43
44 // Set 7-segment display pins as outputs
45 for (int i = 2; i <= 9; i++) {
46 pinMode(i, OUTPUT);
47 }
48
49 // Set oscillator and speaker pins as outputs
50 pinMode(oscillatorPin, OUTPUT);
51 pinMode(speakerPin, OUTPUT);
52}
53
54void loop() {
55 // Read potentiometer value
56 int potValue = analogRead(potPin);
57
58 // Map potentiometer value to frequency range (adjust as needed)
59 int frequency = map(potValue, 0, 1023, 100, 1000);
60
61 // Generate frequency on oscillator and speaker pins
62 tone(oscillatorPin, frequency);
63 tone(speakerPin, frequency);
64
65 // Display frequency on 7-segment display
66 displayFrequencyOnSevenSegment(frequency);
67
68 // Display frequency and pot value on LCD
69 lcd.clear();
70 lcd.setCursor(0, 0);
71 lcd.print("Freq: ");
72 lcd.print(frequency);
73 lcd.print(" Hz");
74
75 lcd.setCursor(0, 1);
76 lcd.print("Pot: ");
77 lcd.print(potValue);
78
79 delay(100); // Reduced delay for smoother sound
80}
81
82// Function to display frequency on 7-segment display
83void displayFrequencyOnSevenSegment(int frequency) {
84 int hundredsDigit = frequency / 100;
85 int tensDigit = (frequency / 10) % 10;
86
87 // Display hundreds digit on the first 7-segment display
88 digitalWrite(10, HIGH); // Enable the first digit (common anode)
89 digitalWrite(11, LOW); // Disable the second digit
90 displayDigit(hundredsDigit);
91 delay(5); // Delay to prevent flickering
92
93 // Display tens digit on the second 7-segment display
94 digitalWrite(10, LOW); // Disable the first digit
95 digitalWrite(11, HIGH); // Enable the second digit (common anode)
96 displayDigit(tensDigit);
97 delay(5); // Delay to prevent flickering
98}
99
100void displayDigit(int digit) {
101 // This function sets the appropriate segment pins LOW or HIGH
102 // to display the given digit on a common anode display.
103 switch (digit) {
104 case 0:
105 digitalWrite(segA, LOW);
106 digitalWrite(segB, LOW);
107 digitalWrite(segC, LOW);
108 digitalWrite(segD, LOW);
109 digitalWrite(segE, LOW);
110 digitalWrite(segF, LOW);
111 digitalWrite(segG, HIGH);
112 digitalWrite(segDP, HIGH); // Decimal point off
113 break;
114 case 1:
115 digitalWrite(segA, HIGH);
116 digitalWrite(segB, LOW);
117 digitalWrite(segC, LOW);
118 digitalWrite(segD, HIGH);
119 digitalWrite(segE, HIGH);
120 digitalWrite(segF, HIGH);
121 digitalWrite(segG, HIGH);
122 digitalWrite(segDP, HIGH); // Decimal point off
123 break;
124 case 2:
125 digitalWrite(segA, LOW);
126 digitalWrite(segB, LOW);
127 digitalWrite(segC, HIGH);
128 digitalWrite(segD, LOW);
129 digitalWrite(segE, LOW);
130 digitalWrite(segF, HIGH);
131 digitalWrite(segG, LOW);
132 digitalWrite(segDP, HIGH); // Decimal point off
133 break;
134 case 3:
135 digitalWrite(segA, LOW);
136 digitalWrite(segB, LOW);
137 digitalWrite(segC, LOW);
138 digitalWrite(segD, LOW);
139 digitalWrite(segE, HIGH);
140 digitalWrite(segF, HIGH);
141 digitalWrite(segG, LOW);
142 digitalWrite(segDP, HIGH); // Decimal point off
143 break;
144 case 4:
145 digitalWrite(segA, HIGH);
146 digitalWrite(segB, LOW);
147 digitalWrite(segC, LOW);
148 digitalWrite(segD, HIGH);
149 digitalWrite(segE, HIGH);
150 digitalWrite(segF, LOW);
151 digitalWrite(segG, LOW);
152 digitalWrite(segDP, HIGH); // Decimal point off
153 break;
154 case 5:
155 digitalWrite(segA, LOW);
156 digitalWrite(segB, HIGH);
157 digitalWrite(segC, LOW);
158 digitalWrite(segD, LOW);
159 digitalWrite(segE, HIGH);
160 digitalWrite(segF, LOW);
161 digitalWrite(segG, LOW);
162 digitalWrite(segDP, HIGH); // Decimal point off
163 break;
164 case 6:
165 digitalWrite(segA, LOW);
166 digitalWrite(segB, HIGH);
167 digitalWrite(segC, LOW);
168 digitalWrite(segD, LOW);
169 digitalWrite(segE, LOW);
170 digitalWrite(segF, LOW);
171 digitalWrite(segG, LOW);
172 digitalWrite(segDP, HIGH); // Decimal point off
173 break;
174 case 7:
175 digitalWrite(segA, LOW);
176 digitalWrite(segB, LOW);
177 digitalWrite(segC, LOW);
178 digitalWrite(segD, HIGH);
179 digitalWrite(segE, HIGH);
180 digitalWrite(segF, HIGH);
181 digitalWrite(segG, HIGH);
182 digitalWrite(segDP, HIGH); // Decimal point off
183 break;
184 case 8:
185 digitalWrite(segA, LOW);
186 digitalWrite(segB, LOW);
187 digitalWrite(segC, LOW);
188 digitalWrite(segD, LOW);
189 digitalWrite(segE, LOW);
190 digitalWrite(segF, LOW);
191 digitalWrite(segG, LOW);
192 digitalWrite(segDP, HIGH); // Decimal point off
193 break;
194 case 9:
195 digitalWrite(segA, LOW);
196 digitalWrite(segB, LOW);
197 digitalWrite(segC, LOW);
198 digitalWrite(segD, LOW);
199 digitalWrite(segE, HIGH);
200 digitalWrite(segF, LOW);
201 digitalWrite(segG, LOW);
202 digitalWrite(segDP, HIGH); // Decimal point off
203 break;
204 default:
205 // Turn off all segments for unknown digits
206 for (int i = 2; i <= 9; i++) {
207 digitalWrite(i, HIGH);
208 }
209 break;
210 }
211}

Documentation

Layout

How to connect this circuit

Understanding voltage and frequency.png




Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.

SendData

Điều khiển trạng thái qua Firebase Trạng thái hiện tại: Đang tải... ĐỔI TRẠNG THÁI