Question: MATLAB Code to Read Magnetic Field Sensor % File to acquire live voltage data from analog port A 0 . % If this produces an

MATLAB Code to Read Magnetic Field Sensor
% File to acquire live voltage data from analog port A0.
% If this produces an error, check the COM port number.
if not(exist('a'))
a=arduino;
end
N=1000; %number of data points
figure; %open a figure window
t=zeros(1,N); %preallocated memory for time array
B=zeros(1,N); %preallocated memory for B-field array
p=animatedline; %create plot
xlabel('time(s)'); %plot axis label
ylabel('Magnetic Field(G)'); %plot axis label
Bbaseline = a.readVoltage("A0"); %Read baseline B field for comparison
t0=cputime; %initial CPU time
%loop to acquire real-time B-field for N iterations
for i=1:N
t(i)=cputime-t0; %update current time
B(i)=(a.readVoltage("A0")-Bbaseline)/0.0014; %update B-field array
addpoints(p, t(i),B(i)); %add latest datum to plot
drawnow; %draw updated plot
end
1. Using your mini breadboard and jumper wires, you will connect three of the four pins on the sensor to the Arduino: G to GND,+ to 5V and AO (analog output) to A0 or any other analog pin that you want to connect it to (so long as the code expects data from that pin).
2. Using the code above (as an m-file in MATLAB), read the highest and lowest values of magnetic field that you get when you place your magnet nearest the field sensor on your little chip (the black protrusion at the top is the sensor). Q3: What is the highest level in gauss (G)? Q4: What is the lowest? Q5: Are they roughly equal and opposite? HINT: Use max or min once you have a B array.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!