Advertisement
microrobotics

JY-VC01 LM393 general-purpose voltage comparator module

Apr 15th, 2023
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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:
  3.  
  4. 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.
  5.  
  6. 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.
  7.  
  8. 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.
  9. */
  10.  
  11. // Arduino code for JY-VC01 LM393 Voltage Comparator Module
  12.  
  13. const int analogPin = A0;  // Analog input pin connected to the input voltage
  14. const int comparatorPin = 2;  // Digital input pin connected to the comparator output
  15.  
  16. float referenceVoltage = 2.5;  // Reference voltage in volts (adjust as needed)
  17.  
  18. void setup() {
  19.   pinMode(comparatorPin, INPUT);
  20.   Serial.begin(9600);
  21. }
  22.  
  23. void loop() {
  24.   int analogValue = analogRead(analogPin);
  25.   float inputVoltage = (analogValue / 1023.0) * 5.0;
  26.  
  27.   int comparatorState = digitalRead(comparatorPin);
  28.  
  29.   Serial.print("Input voltage: ");
  30.   Serial.print(inputVoltage);
  31.   Serial.println(" V");
  32.  
  33.   if (comparatorState == HIGH) {
  34.     Serial.println("Input voltage is higher than reference voltage");
  35.   } else {
  36.     Serial.println("Input voltage is lower than or equal to reference voltage");
  37.   }
  38.  
  39.   Serial.println();
  40.   delay(1000);
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement