Hans ScharlerRobert Mawrey
Published © MIT

Measure and Analyze Tide Levels with ThingSpeak and MATLAB

Learn how to create a tide gauge that publishes tidal water levels to ThingSpeak and uses MATLAB for tide prediction, analysis, and alerts.

AdvancedFull instructions provided8 hours17,007
Measure and Analyze Tide Levels with ThingSpeak and MATLAB

Things used in this project


Read more


Tide Sensor


Detect Tidal Thresholds

% This code reads the past 30 minutes tide range data from ThingSpeak
% channel 22641. Calculates the median of the tide level 5 minutes ago and
% 25 minutes ago.  It then uses these values to check if either a) the tide
% is going down and has passed a defined 'falling threshold' or b) is going
% up and has passed a defined 'rising threshold'. When either of these
% thresholds is passed the tide height when that happens is written to a
% ThingSpeak Falling Tide or Rising Tide channel as appropriate. The code
% also checks to make sure that sufficient time has past before writing a
% new value to avoid multiple triggers for the same threshold.

% Read the last 30 minutes of tide data
range = thingSpeakRead(22641,'Fields',[1],'NumMinutes',30, ...

% Convert range from sensor to water in mm to depth from mud in inches
depth = (3449 - range)/24.5; %depth from mud in inches

% Define threshold level in inches
fallingthreshold = 20;
risingthreshold = 15;

% Define minimum time in minutes between alerts
alertGap = 60; % minutes
alertGapNum = alertGap/60/24; % Convert to datenum

% Set up index to divide data into roughly 10 minute periods
i = round(length(depth)/3);

% Make sure we are working with enough data
if i>8

    % Calculate the median of each 10 minute period
    depth25minago = median(depth(1:i));
    depth5minago = round(median(depth(2*i+1:end)),1);

    % Check time since last change used below to avoid double triggers
    timeNow = datetime('now');

    % Read the last falling time
    [lastFallingHeight,lastFallingTime] = thingSpeakRead(50192,...
        'ReadKey', '9QSZ88F42HE2BA4H',...
    if isempty(lastFallingTime) % at the start this will be empty
        sinceLastFall = 1;
        sinceLastFall = datenum(timeNow)-datenum(lastFallingTime);
    % Read the last rising time
    [lastRisingHeight,lastRisingTime] = thingSpeakRead(50193,...
        'ReadKey', '9AL2W9R0Y07GFR02',...
    if isempty(lastRisingTime) % at the start this will be empty
        sinceLastRise = 1;
        sinceLastRise = datenum(timeNow)-datenum(lastRisingTime);

        % If the tide has fallen below the falling threshold write to the
        % ThingSpeak falling tide channel
        if (depth25minago > fallingthreshold)...
            && (depth5minago <= fallingthreshold)...
            && (sinceLastFall > alertGapNum)
            thingSpeakWrite(50192, depth5minago, ...
                'WriteKey', '928PKIJS64PXECSL',...

        % If the tide has fallen below the rising threshold write to the
        % ThingSpeak rising tide channel
        if (depth25minago < risingthreshold)...
            && (depth5minago >= risingthreshold)...
            && (sinceLastRise > alertGapNum)
            thingSpeakWrite(50193, depth5minago,...
                'WriteKey', 'WYVR5N22N4NABWR6',...


Tide Alerts on GitHub


Hans Scharler

Hans Scharler

15 projects • 85 followers
IoT Engineer, Maker - I have a toaster that has been tweeting since 2008.
Robert Mawrey

Robert Mawrey

2 projects • 31 followers
Founder and CEO of Sentient Things, Inc. Former CEO of IoT startup ioBridge.com, the creators of ThingSpeak.com.