Push button and turn on LED diode Description

This example shows how to control Red Pitaya on board LEDs and read states of extension connector GPIOs. LED will turn ON, when button is pressed. Required hardware

  • Red Pitaya device

  • Push button

  • Resistor 1K

  • RedPitaya_Push_button

Wiring example for STEMlab 125-14 & STEMlab 125-10: .. figure:: RedPitaya_Push_button.png


../../_images/RedPitaya_Push_button_circuit.png Code - MATLAB®

The code is written in MATLAB. In the code we use SCPI commands and TCP/IP communication. Copy code from below to MATLAB editor, save project and press run.

%% Define Red Pitaya as TCP/IP object

IP= '';           % Input IP of your Red Pitaya...
port = 5000;
tcpipObj=tcpip(IP, port);

%% Open connection with your Red Pitaya

tcpipObj.Terminator = 'CR/LF';

fprintf(tcpipObj,'DIG:PIN:DIR IN,DIO5_N'); % Set DIO5_N  to be input


while i<1000                                        % You can set while 1 for continuous loop

state=str2num(query(tcpipObj,'DIG:PIN? DIO5_N'));

    if state==1

    fprintf(tcpipObj,'DIG:PIN LED5,0');


    if state==0

    fprintf(tcpipObj,'DIG:PIN LED5,1');


pause(0.1)                                                  % Set time delay for Red Pitaya response



%% Close connection with Red Pitaya
fclose(tcpipObj); Code - C


C code examples don’t require the use of the SCPI server, we have included them here to demonstrate how the same functionality can be achieved with different programming languages. Instructions on how to compile the code are here -> link

#include <stdio.h>
#include <stdlib.h>

#include "rp.h"

int main (int argc, char **argv) {
    rp_pinState_t state;

    // Initialization of API
    if (rp_Init() != RP_OK) {
        fprintf(stderr, "Red Pitaya API init failed!\n");
        return EXIT_FAILURE;

    // configure DIO[0:7]_N to inputs
    for (int i=0; i<8; i++) {
        rp_DpinSetDirection (i+RP_DIO0_N, RP_IN);

    // transfer each input state to the corresponding LED state
    while (1) {
        for (int i=0; i<8; i++) {
            rp_DpinGetState (i+RP_DIO0_N, &state);
            rp_DpinSetState (i+RP_LED0, state);

    // Releasing resources

    return EXIT_SUCCESS;
} Code - Python


import sys
import redpitaya_scpi as scpi

rp_s = scpi.scpi(sys.argv[1])

# set all DIO*_N pins to inputs
for i in range(8):
    rp_s.tx_txt('DIG:PIN:DIR IN,DIO'+str(i)+'_N')

# copy DIOi_N pin state to LEDi state fir each i [0:7]
while 1:
    for i in range(8):
        rp_s.tx_txt('DIG:PIN? DIO'+str(i)+'_N')
        state = rp_s.rx_txt()
        rp_s.tx_txt('DIG:PIN LED'+str(i)+','+str(state)) Code - LabVIEW