Blink
Description
This example shows how to control one of the Red Pitaya on-board LEDs and make it blink.
Required hardware
Red Pitaya device
Required software
2.00-23 or higher OS
Note
This code is written for 2.00-23 or higher OS. For older OS versions, please check when specific commands were released (a note is added to each command introduced in 2.00 or higher verisons).
SCPI Code Examples
Code - MATLAB ®
The code is written in MATLAB. TCP client communication is used to establish socket communication with Red Pitaya, then SCPI commands are sent to configure the various Red Pitaya peripherals. Copy the code below into the MATLAB editor, save the project and press the Run button. Tested on MATLAB 2024b.
%% Define Red Pitaya as TCP/IP object
IP = ('rp-f0a235.local'); % Input IP of your Red Pitaya...
port = 5000;
RP = tcpclient(IP, port); % creates a TCP client object
%% Open connection with your Red Pitaya
RP.ByteOrder = "big-endian";
configureTerminator(RP, 'CR/LF'); % defines the line terminator (end sequence of input characters)
%% Send SCPI command to Red Pitaya to turn ON LED1
for i=1:5
writeline(RP,'DIG:PIN LED1,1'); % Peripheral_Unit: Unit_Part/function:subfunction/settings
% readline() % reading data
% writeread() % send a command and read the reply
pause(1); % Set time of LED ON
% Send SCPI command to Red Pitaya to turn OFF LED1
writeline(RP,'DIG:PIN LED1,0');
% other possible commands:
% DIG:PIN:DIR <dir>,<gpio>
% DIG:PIN <pin>,<state>
% DIG:PIN? <pin> => <state> % Acquire status or read data
pause(1);
end
%% Close connection with Red Pitaya
clear RP;
Code - Python
#!/usr/bin/env python3
import sys
import time
import redpitaya_scpi as scpi
IP = 'rp-f066c8.local'
rp = scpi.scpi(IP)
if (len(sys.argv) > 2):
led = int(sys.argv[2])
else:
led = 0
print ("Blinking LED["+str(led)+"]")
period = 1 # seconds
while 1:
time.sleep(period/2.0)
rp.tx_txt('DIG:PIN LED' + str(led) + ',' + str(1))
time.sleep(period/2.0)
rp.tx_txt('DIG:PIN LED' + str(led) + ',' + str(0))
rp.close()
Note
The Python functions are accessible with the latest version of the redpitaya_scpi.py document available on our GitHub. The functions represent a quality-of-life improvement as they combine the SCPI commands in an optimal order and also check for improper user inputs. The code should function at approximately the same speed without them.
For further information on functions please consult the redpitaya_scpi.py code.
Code - LabVIEW
API Code Examples
Note
The API code examples don’t require the use of the SCPI server. Instead, the code should be compiled and executed on the Red Pitaya itself (inside Linux OS). Instructions on how to compile the code and other useful information are here.
Code - C++ API
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "rp.h"
int main (int argc, char **argv) {
int unsigned period = 1000000; // uS
int unsigned led;
// index of blinking LED can be provided as an argument
if (argc > 1) {
led = atoi(argv[1]);
// otherwise LED 0 will blink
} else {
led = 0;
}
printf("Blinking LED[%u]\n", led);
led += RP_LED0;
// Initialization of API
if (rp_Init() != RP_OK) {
fprintf(stderr, "Red Pitaya API init failed!\n");
return EXIT_FAILURE;
}
int unsigned retries = 1000;
while (retries--){
rp_DpinSetState(led, RP_HIGH);
usleep(period/2);
rp_DpinSetState(led, RP_LOW);
usleep(period/2);
}
// Releasing resources
rp_Release();
return EXIT_SUCCESS;
}
Code - Python API
#!/usr/bin/python3
import time
import rp
period = 1 # period in secodns
# Initialize the interface
rp.rp_Init()
#####! Choose one of two methods, comment the other !#####
#! METHOD 1: Interacting with Registers direclty
while 1:
time.sleep(period/2.0)
rp.rp_LEDSetState(0b00000001) # or 0b00000001
time.sleep(period/2.0)
rp.rp_LEDSetState(0b00000000) # or 0
#! METHOD 2: Using Macros
while 1:
time.sleep(period/2.0)
rp.rp_DpinSetState(rp.RP_LED0, rp.RP_HIGH)
time.sleep(period/2.0)
rp.rp_DpinSetState(rp.RP_LED0, rp.RP_LOW)
# Release resources
rp.rp_Release()