Reading Bytes from Arduino to MATLAB (2024)

20 views (last 30 days)

Show older comments

Nicholas Ross on 29 Aug 2024 at 21:20

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2148849-reading-bytes-from-arduino-to-matlab

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2148849-reading-bytes-from-arduino-to-matlab

Commented: Nicholas Ross about 19 hours ago

Open in MATLAB Online

I've been trying to read data from ESP32S-Dev module to MATLAB sending over the data in bytes to make the transmission quicker. As seen below, I convert float data into bytes and then write the data. MATLAB sees the data and then creates a 1x8 array for each float value instead of just 1 value for each float. I've tried other methods, shown in 2nd part of MATLAB code, and it creates 1 value for each float but their wildly off (7-34 orders of magnitude)

I've tried different baud rates and even asked for help from the almighty ChatGPT but have not been able to crack the code. Any thoughts.

Arduino IDE code:

#include <SPI.h>

const int CS_SiPM_pot = 34;

void setup() {

// put your setup code here, to run once:

Serial.begin(115200); //Starting

delay(1000);

SPI.begin();

pinMode(CS_SiPM_pot, OUTPUT);

}

void loop() {

for (int d_pot2 = 55; d_pot2 <= 55; d_pot2 = d_pot2 + 1) //57

{

digitalWrite(CS_SiPM_pot, LOW); //LOW

delay(10); //10

SPI.transfer(d_pot2);

digitalWrite(CS_SiPM_pot, HIGH);

// put your main code here, to run repeatedly:

float newTmp[4] = { 01.00, 02.00, 03.00, 04.00 };

for (int i = 0; i < 4; i++) {

byte *byteData = (byte *)&newTmp[i];

Serial.write(byteData, sizeof(float)); // Send each float as 4 bytes

delay(10);

}

}

}

MATLAB code:

esp = serialport('COM5',152000);

%%

flush(esp)

% Read 16 bytes of data (4 floats * 4 bytes per float)

numBytes = 16;

data = read(esp, numBytes, 'uint8');

% Convert each 4-byte sequence to a float

float1 = typecast(data(1:4)), 'single');

float2 = typecast(data(5:8), 'single');

float3 = typecast(data(9:12), 'single');

float4 = typecast(data(13:16), 'single');

disp([float1, float2, float3, float4]);

% Convert the bytes back to float values

floatValues = typecast(uint8(data), 'single');

% Display the result

disp('Float values:');

disp(floatValues);

disp('Raw bytes received:');

disp(data); % Print the raw bytes

Results from MATLAB:

Float1: 00 0 0 0 0 0 0

Float2: 0 3.7480 0 0 0 0 0 0

Float3: 0 2.1250 0 3.7480 0 0 0 0

Float4: 0 0 0 0 0 3.7480 0 0

data: 0000255000325500002550

2 Comments

Show NoneHide None

Walter Roberson on 30 Aug 2024 at 1:55

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2148849-reading-bytes-from-arduino-to-matlab#comment_3249509

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2148849-reading-bytes-from-arduino-to-matlab#comment_3249509

Open in MATLAB Online

float1 = typecast(data(1:4)), 'single');

You have an extra ) in there.

Nicholas Ross about 19 hours ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2148849-reading-bytes-from-arduino-to-matlab#comment_3249924

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2148849-reading-bytes-from-arduino-to-matlab#comment_3249924

@Walter Roberson thanks for the heads up!

Sign in to comment.

Sign in to answer this question.

Answers (2)

dpb on 29 Aug 2024 at 22:14

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2148849-reading-bytes-from-arduino-to-matlab#answer_1507394

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2148849-reading-bytes-from-arduino-to-matlab#answer_1507394

Edited: dpb on 30 Aug 2024 at 1:24

Open in MATLAB Online

I dunno how to fix the Arduino side, but the data bytes you're returning aren't the right values for the single float values 1 thru 4.

x=single(1:4).';

format hex

disp(x)

3f800000 40000000 40400000 40800000

bytes=reshape(typecast(x,'uint8'),4,[]).'

bytes = 4x4

00 00 80 3f 00 00 00 40 00 00 40 40 00 00 80 40

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

format short

bytes

bytes = 4x4

0 0 128 63 0 0 0 64 0 0 64 64 0 0 128 64

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

arrayfun(@(i)typecast(bytes(i,:),'single'),[1:height(bytes)].')

ans = 4x1

1 2 3 4

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

The data array you received back should have been

data=reshape(bytes.',1,[])

data = 1x16

0 0 128 63 0 0 0 64 0 0 64 64 0 0 128 64

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Walter Roberson on 30 Aug 2024 at 1:57

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2148849-reading-bytes-from-arduino-to-matlab#answer_1507424

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2148849-reading-bytes-from-arduino-to-matlab#answer_1507424

Open in MATLAB Online

https://www.mathworks.com/help/instrument/serialport.read.html

data = read(device,count,datatype) reads the number of values specified by count in the form specified by datatype from the serial port connection device. For all numeric datatype types, data is a row vector of double values.

You still need the 'uint8' to specify how to read the data, but you should be using

data = uint8(read(esp, numBytes, 'uint8'));

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

Test and MeasurementInstrument Control ToolboxInterface-Based Instrument CommunicationSerial Port Interface

Find more on Serial Port Interface in Help Center and File Exchange

Tags

  • arduino
  • esp32
  • matlab
  • serialport
  • float-to-bytes

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Reading Bytes from Arduino to MATLAB (6)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Reading Bytes from Arduino to MATLAB (2024)
Top Articles
Usssa.com
Pawn Shops Close To Me
Blackstone Launchpad Ucf
Gfr Soccer
Honda Odyssey Questions - P0303 3 cyclinder misfire
Mimissliza01
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Fbsm Berkeley
North Station To Lowell Schedule
Google Sites 1V1.Lol
Jeff Siegel Picks Santa Anita
Guy I'm Talking To Deleted Bumble
Sevita Sso Login
Best Bread for Gut Health
Randolph Leader Obits
Apryl Prose Wiki
Wat is 7x7? De gouden regel voor uw PowerPoint-presentatie
Body Rub Phoenix
Best Stb 556 Mw2
Jacy Nittolo Ex Husband
Acuity Eye Group - La Quinta Photos
Fgo Spirit Root
Evil Dead Rise Showtimes Near Cinemark Movies 10
Cal Poly San Luis Obispo Catalog
Nephi Veterinarian
Dash Ag Grid
Gulfport Senior Center Calendar
Weer Maasbracht - Vandaag - Morgen - 14 dagen
11 Nightlife Spots To Experience In Salem, Oregon
Карта слов и выражений английского языка
Jasper Jones County Trade
Roomba I3 Sealing Problem With Clean Base
Log in or sign up to view
Search results for: Kert\u00E9sz, Andr\u00E9, page 1
Sentara Norfolk General Visiting Hours
Mula Pelada
Media Press Release | riversideca.gov
Rexella Van Impe Net Worth
MAXSUN Terminator Z790M D5 ICE Motherboard Review
2Nd Chance Apartments In Richmond Va
Puppies For Sale in Netherlands (98) | Petzlover
30 Day Long Range Weather for 82801 (Sheridan), Wyoming. Weather Outlook for 30 Days From Today.
Ny Lottery Second Chance App
Mcoc Black Panther
Craigslist Garage Sales Schenectady Ny
Breitling ENDURANCE PRO X82310E51B1S1 für 2.885 € kaufen von einem Trusted Seller auf Chrono24
Craigslist Nokomis Fl
Ascensionpress Com Login
Westside Veterinary Hospital Arab Photos
Craigslist Groton
3220 Nevada Terrace Ottawa Ks 66067
Bme Flowchart Psu
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6297

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.