Arduino readVoltage not working

18 views (last 30 days)
Ethan
Ethan on 17 Oct 2023
I'm trying to read the voltage from an analog pin on an arduino leonardo for a university project. Currently I'm just working in the MATLAB command window and i type a = arduino() which works but when I type readVoltage(a, 'A0') I get one of two errors. Usually I get "Specified key type does not match the type expected for this container." but sometimes I get "The first input must contain a multiple of 2 elements to convert from uint8 (8 bits) to uint16 (16 bits).". I'm copying exactly what i see online word for word so why isnt it working?

Answers (2)

Aman Banthia
Aman Banthia on 25 Oct 2023
Hi Ethan,
I understand you are trying to read the voltage from an analog pin (A0) on an Arduino Leonardo using MATLAB. You're creating an Arduino object in MATLAB using the command ‘a = arduino()’, which is working fine. However, when you're trying to read the voltage using the command ‘readVoltage(a, 'A0')’, you're encountering two different errors.
  • "Specified key type does not match the type expected for this container." This error usually occurs when you're trying to access an element in a container (like a Dictionary or Map) using a key of an incorrect type. In this case, it could be that the 'A0' you're using to specify the analog pin might not be correct.
  • "The first input must contain a multiple of 2 elements to convert from uint8 (8 bits) to uint16 (16 bits)." It's saying that some function is trying to convert an 8-bit unsigned integer (uint8) to a 16-bit unsigned integer (uint16), but the input isn't compatible.
Without more information about your setup, it's hard to diagnose exactly what's going wrong. However, here are some steps you could try:
Check your Arduino board type: Make sure that you're specifying the correct board type when you create the Arduino object in MATLAB. For an Arduino Leonardo, it should be:
a = arduino('COM port', 'Leonardo');
Replace 'COM port' with the actual COM port your Arduino is connected to. You can find this in the Arduino IDE under 'Tools' -> 'Port'.
Check your pin specification: Make sure you're specifying the pin correctly. For analog pin A0, it should be:
voltage = readVoltage(a, 'A0');
If the above steps don't solve your problem, it could be an issue with the MATLAB support package for Arduino itself or the communication between MATLAB and your Arduino board. In this case, you might want to try reinstalling the support package, or testing your Arduino board with a simple sketch in the Arduino IDE to make sure it's working correctly.
Please refer to the following MATLAB documentation link to know more about the MATLAB Support Package for Arduino Hardware:
Hope the above answer helps.
Best Regards,
Aman Banthia

MathWorks MATLAB Hardware Team
Hello,
Thank you for contacting us. We apologize for the inconvenience you are experiencing.
To resolve this issue, we kindly request that you reach out to our technical support team. They will be able to provide you with the necessary assistance.
https://www.mathworks.com/support/contact_us.html
Thanks,
MATLAB Hardware Team
MathWorks

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!