2.3.6.2.2. Set analog voltage on a slow analog output

2.3.6.2.2.1. Description

This example shows how to set the analog voltage of slow analog outputs on the Red Pitaya extension connector. Slow analog outputs on the Red Pitaya are in the range of 0 to 1.8 Volts.

2.3.6.2.2.2. Required hardware

  • Red Pitaya device

  • Voltmeter

Wiring example for STEMlab 125-14 & STEMlab 125-10:

../../../_images/Set_analog_voltage_on_slow_analog_input1.png

2.3.6.2.2.3. Circuit

../../../_images/Set_analog_voltage_on_slow_analog_input_circuit1.png

2.3.6.2.2.4. SCPI Code Examples

2.3.6.2.2.4.1. Code - MATLAB®

The code is written in MATLAB. In the code, we use SCPI commands and TCP client communication. Copy the code from below into the MATLAB editor, save the project, and hit the “Run” button.

%% Define Red Pitaya as TCP client object
IP= '192.168.178.108';          % Input IP of your Red Pitaya...
port = 5000;
RP = tcpclient(IP, port);

%% Open connection with your Red Pitaya

RP.ByteOrder = "big-endian";
configureTerminator(RP,"CR/LF");

writeline(RP,'ANALOG:PIN AOUT0,0.3');  % 0.3 Volts is set on output 0
writeline(RP,'ANALOG:PIN AOUT1,0.9');
writeline(RP,'ANALOG:PIN AOUT2,1');
writeline(RP,'ANALOG:PIN AOUT3,1.5');

clear RP;

2.3.6.2.2.4.2. Code - Python

#!/usr/bin/env python3

import sys
import redpitaya_scpi as scpi

IP = 'rp-f066c8.local'

rp_s = scpi.scpi(IP)

value = [1,1,1,1]
for i in range(4):
    if len(sys.argv) > (i+2):
        value[i] = sys.argv[i+2]
    print ("Voltage setting for AO["+str(i)+"] = "+str(value[i])+"V")

for i in range(4):
    rp_s.tx_txt('ANALOG:PIN AOUT' + str(i) + ',' + str(value[i]))

rp_s.close()

2.3.6.2.2.4.3. Code - LabVIEW

../../../_images/Set-analog-voltage-on-slow-analog-output_LV.png

2.3.6.2.2.5. 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.

2.3.6.2.2.5.1. Code - C API

/* Set analog voltage on slow analog output */

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

#include "rp.h"

int main (int argc, char **argv) {
    float value [4];

    // Voltages can be provided as an argument (default is 1 V)
    for (int i=0; i<4; i++) {
        if (argc > (1+i)) {
            value [i] = atof(argv[1+i]);
        } else {
            value [i] = 1.0;
        }
        printf("Voltage setting for AO[%i] = %1.1fV\n", i, value [i]);
    }

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

    // Setting a voltage for each ananlog output
    for (int i=0; i<4; i++) {
        int status = rp_AOpinSetValue(i, value[i]);
        if (status != RP_OK) {
            printf("Could not set AO[%i] voltage.\n", i);
        }
    }

    // wait for user input
    getchar();

    // Releasing resources
    rp_Release();

    return EXIT_SUCCESS;
}

2.3.6.2.2.5.2. Code - Python API

#!/usr/bin/python3
import rp

analog_out = [rp.RP_AOUT0, rp.RP_AOUT1, rp.RP_AOUT2, rp.RP_AOUT3]
out_voltage = [1.0, 1.0, 1.0, 1.0]

# Initialize the interface
rp.rp_Init()

# Reset analog pins
rp.rp_ApinReset()

#####! Choose one of two methods, comment the other !#####

#! METHOD 1: Configuring specific Analog pin
for i in range(4):
    rp.rp_ApinSetValue(analog_out[i], out_voltage[i])
    print (f"Set voltage on AO[{i}] to {out_voltage[i]} V")


#! METHOD 2: Configure just slow Analog outputs
for i in range(4):
    rp.rp_AOpinSetValue(i, out_voltage[i])
    print (f"Set voltage on AO[{i}] to {out_voltage[i]} V")

# Release resources
rp.rp_Release()