pythonvisual-studio-codearduinocircuit

Arduino Id Instance error on python vscde


Whenever I try to run a code that is supposed to transmit a signal using Sonar HC-SR04 module and then receive that signal to ouput the distance. But as soon as I run the code, it stops immediately and this error pops up :

Opening all potential serial ports... COM3

Waiting 4 seconds(arduino_wait) for Arduino devices to reset...

Searching for an Arduino configured with an arduino_instance = 1
Traceback (most recent call last):
  File "d:\Hassaan\MarvinPythonTest.py", line 4, in <module>
    board = pymata4.Pymata4()
            ^^^^^^^^^^^^^^^^^
  File "C:\Users\kk198\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymata4\pymata4.py", line 235, in __init__
    self._find_arduino()
  File "C:\Users\kk198\AppData\Local\Programs\Python\Python311\Lib\site-packages\pymata4\pymata4.py", line 404, in _find_arduino
    raise RuntimeError(f'arduino_instance_id does not match '
RuntimeError: arduino_instance_id does not match a value on the boards.

I've tried putting the port back, resetting the Arduino, fixing the code, (although the code works on the Arduino ide), updating the libraries, reinstalling pymata4, restarting my laptop, and changing the pins, but nothing has worked.

from pymata4 import pymata4
import time

board = pymata4.Pymata4()

# Define the pins
trig_pin = 8
echo_pin = 9

# Configure the pins
board.set_pin_mode(trig_pin, board.OUTPUT)
board.set_pin_mode(echo_pin, board.INPUT)

while True:
# Set the trigger pin HIGH
board.digital_write(trig_pin, 1)

# Wait for 10 microseconds
time.sleep(0.00001)

# Set the trigger pin LOW
board.digital_write(trig_pin, 0)

# Wait for the echo_pin to go HIGH
while board.digital_read(echo_pin) == 0:
    start_time = time.time()

# Wait for the echo_pin to go LOW
while board.digital_read(echo_pin) == 1:
    end_time = time.time()
    
# Calculate duration and distance
duration = end_time - start_time
distance = duration * 34300 / 2  # Using 34300 cm/s as the speed             of sound

print(f"Distance: {distance:.2f} cm")

# Wait for 1 second before the next loop
time.sleep(1)

Solution

  • Just reupload firmataezpress and it will work