VideotronicMaker
Published © CC BY

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.

IntermediateProtip1 hour360
Arduino Voltage and Frequency Simulation | Tinkercad

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1
I2C 16x2 Arduino LCD Display Module
DFRobot I2C 16x2 Arduino LCD Display Module
×1
7 Segment LED Display, InfoVue
7 Segment LED Display, InfoVue
×1
Buzzer
Buzzer
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×8
oscillator
×1

Software apps and online services

Tinkercad
Autodesk Tinkercad
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Layout

Schematic

Code

voltage_frequency_demo

Arduino
The fundamental relationship between voltage and frequency in electronic circuits. We'll use a Tinkercad simulation to demonstrate how changing the voltage affects the frequency of an oscillator. This approach allows us to visualize and experiment with this concept even without physical components.
/*
  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 13 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;
  }
}

Credits

VideotronicMaker
10 projects • 8 followers
A co-worker told me about Arduino. I researched it all and I was fascinated by what I saw. I have now begun my journey as a maker.
Contact
Thanks to Google Gemini Advanced.

Comments

Please log in or sign up to comment.