Skip to content

Instantly share code, notes, and snippets.

@futureshocked
Created April 26, 2021 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save futureshocked/bc554c74773aab897805aa3e7a3329c7 to your computer and use it in GitHub Desktop.
Save futureshocked/bc554c74773aab897805aa3e7a3329c7 to your computer and use it in GitHub Desktop.
This script creates a rudimentary sine wave on DAC0.
--[[
LJ - 07.30 - sine_wave_DAC0.lua
This script creates a rudimentary sine wave on DAC0.
Components
----------
- LabJack T4
- Oscilloscope
- GND probe to GND
- Signal probe to DAC0
- Wires
- Breadboard
Course
------
Data acquisition and automation with LabJack
https://app.techexplorations.com/courses/labjack/
--]]
print("Output sine wave. Analog output is DAC0.")
local OutputVoltage = 0
local mbWrite=MB.W
--If max amplitude is needed, use 2.5V centerline and 2.25V amplitude. VS ranges from 4.75-5.00V depending on setup.
local amplitude = 2 --amplitude in volts (maximum of 5V for VS)
local offset = 2.5 --offset, or centerline voltage in volts
local frequency = 100 --frequency in Hz
local rad_step = .01 --radians per step. A smaller number increases waveform resolution
local timer_ms = 1000 / (2 * (frequency / rad_step) )
local rads = 0
LJ.IntervalConfig(0, timer_ms) --set interval to 10 for 10ms
local checkInterval=LJ.CheckInterval
while true do
if checkInterval(0) then --interval completed
if rads >= 2 then
rad_step = rad_step * -1
end
OutputVoltage = ( (math.sin(rads) ) * amplitude) + offset
rads = rads + rad_step
mbWrite(1000, 3, OutputVoltage) --Set DAC0. Address is 1000, type is 3
--print(OutputVoltage)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment