Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The JY-VC01 LM393 voltage comparator module is a simple module that can be used to compare two voltages and provide a digital output based on the comparison. Here's an example of how to use the JY-VC01 LM393 module with an Arduino to compare an input voltage to a reference voltage and display the result on the serial monitor:
- In this example code, we connect the input voltage that we want to compare to an analog pin (A0) and the output of the comparator module to a digital pin (D2). The referenceVoltage variable is set to the desired reference voltage (2.5V in this case), which can be adjusted as needed.
- The loop function reads the input voltage using analogRead and converts it to a voltage value. It then reads the comparator output using digitalRead. If the comparator output is HIGH, it indicates that the input voltage is higher than the reference voltage; otherwise, the input voltage is lower than or equal to the reference voltage. The input voltage and comparison result are printed to the serial monitor.
- Note: This example assumes you have already connected the JY-VC01 LM393 module and set the reference voltage according to the manufacturer's recommendations. Be sure to consult the module's documentation for proper wiring and usage instructions.
- */
- // Arduino code for JY-VC01 LM393 Voltage Comparator Module
- const int analogPin = A0; // Analog input pin connected to the input voltage
- const int comparatorPin = 2; // Digital input pin connected to the comparator output
- float referenceVoltage = 2.5; // Reference voltage in volts (adjust as needed)
- void setup() {
- pinMode(comparatorPin, INPUT);
- Serial.begin(9600);
- }
- void loop() {
- int analogValue = analogRead(analogPin);
- float inputVoltage = (analogValue / 1023.0) * 5.0;
- int comparatorState = digitalRead(comparatorPin);
- Serial.print("Input voltage: ");
- Serial.print(inputVoltage);
- Serial.println(" V");
- if (comparatorState == HIGH) {
- Serial.println("Input voltage is higher than reference voltage");
- } else {
- Serial.println("Input voltage is lower than or equal to reference voltage");
- }
- Serial.println();
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement