Constant Current Electronic Load Controlled by Arduino Manager
by Fab64 in Circuits > Microcontrollers
34 Views, 0 Favorites, 0 Comments
Constant Current Electronic Load Controlled by Arduino Manager
This Instructable will guide you through designing a low-cost constant-current electronic load with minimal components controlled using Raspberry Pico W and Arduino Manager.
The system is made of three components:
- voltage controlled constant current load: a set of electronic components configured to draw a constant current from the connected load. The current can be adjusted setting the control voltage.
- Raspberry Pico W: Receives the user commands, sets the desired current and performs all voltages and current measurements
- Arduino Manager: Remote user interface used to set the current and control the entire system.
Arduino Manager, is a powerful general purpose iOS iPadOS / watchOS / macOS App which allows to control any Arduino (or Arduino compatible) board by the means of about 40 different Widgets.
Some of the available Widgets are:
- Display Widget
- LED Widget
- Switch Widget
- Push Button Widget
- Knob Widget
- Slider Widget
- Alarm Widget
- Sound Alarm Widget
- Thermostat Widget
- Threshold Widget
- Text Widget
- and many more
Arduino Manager prioritizes your privacy, with no registration or cloud account required—only your Apple ID. However, you have the option to save your Widget Lists to iCloud, enabling seamless sharing across your devices.
For more information about Arduino Manager:
- Arduino Manager for iOS, Arduino Manager for macOS.
- Past Instructables:
- How to Control Arduino R4 WiFi From Your IPhone / Apple Watch / IPad / Mac
- Cheap IOT Device in Minutes and Without Writing Any Code Using Raspberry Pico Pi W
- Controlling ESP32 Running Micropython With Arduino Manager
- Arduino Manager: Generate Raspberry Pi Pico C/C++ Code for an AC Power Monitor – Step-by-Step Example
- How to Measure a Remote Temperature Using Pico W and Arduino Manager
When the complexity of the Arduino code increases, such as when dealing with multiple analog and digital sensors to read, controlling numerous actuators, and implementing complex algorithms, the likelihood of making errors rises. Consequently, debugging the code becomes more challenging and the time and effort required to address these issues could significantly affect the progress of your project. For this reason Arduino Manager also provides (for an additional small fee):
- Code Generator: This add-on allows to generate the basic Arduino code for supporting the Widgets you need in your project.
- PIN Code Generator: Linking widgets to Arduino PINs, this add-on dramatically speeds up Arduino code development. In many cases you don't need to write a single line of code.
A nice additional feature is Voice Commands which allows to control most of the Arduino Manager Widgets by means of your voice (English only)!. See past Instructables form more.
Apple Watch is supported as well (with some limitations): you can control any Arduino board directly from your wrist, wherever you are in the world!
Arduino Manager for iOS/iPadOS is available here: App Store for $7.99 and includes the watchOS version. The macOS version is available here: App Store for $10.99 (please, note that not all the widgets are available on all the platforms).
More information, documentation, video tutorials, libraries and examples on Arduino Manager are available here:
Arduino Manager for iOS or here: Arduino Manager for macOS.
For this Instructable you need Arduino Manager version 23.2.0 or greater.
Supplies
- Power supply 9-12V
- Power supply max 30V, 2A
- Breadboard
- Wires
- Schottky Diode 1N5819
- LM324 operation amplifier
- Resistors:
- 1Ω, 1%, at least 2W (better higher)
- 10K, 1%, 1/4W
- 30K, 1%, 1/4W
- 3K, 1%, 1/4W
- 1uF Capacitor (25V)
- NTC (Vishay NTCALUG02A103FA) (any similar NTC will work as well adapting the code)
- MOSFET IRFZ44N (any similar MOSFET that can handle at least 2 A will work as well)
- BS170 (any similar MOSFET will work as well)
- Heatsink
- Fan 5V
- Raspberry Pico W (Pico 2W will work as well)
Constant Current Electronic Load - the Basics Explained
The figure shows the schematic of the basic circuit for the constant-current electronic load.
The working principle is easy to understand.
Since the operation amplifier will do everything it can to keep the voltages at inverting and not inverting inputs equal, we can assume that the voltage across the feedback resistor Rf is equal to Control Voltage.
Therefore the current through Rf is:
Control Voltage
IRf = -------------------
Rf
IRf is the same current drawn from the external load (connected at J2) and is independent of the voltage across the load.
To set the desired current, we adjust the control voltage according to the previous equation.
This is what makes it a constant-current electronic load.
The Control Voltage - Pwm to DC Conversion
To generate the control voltage there are two options:
- A DAC (digital voltage converter): A device that accepts a digital input and converts it into an analog signal. These devices are easy to use and well suited for this purpose, but they are relatively expensive.
- PWM-to-DC conversion. The Pi Pico W can easily generate a PWM signal on any GPIO pin. By Filtering the PWM signal with a low-pass filter, we can get a DC voltage proportional to VH (logic high voltage) and to the PWM duty cycle:
Vdc = VH * Duty Cycle
(See this for a deeper explanation Low-Pass Filter a PWM signal into an Analog Voltage).
Using R = 10K and C = 1uF for the filter is sufficient to obtain a smooth signal for our purpose.
To avoid loading the filter and altering its characteristic, it is best to follow it with a buffer that has a very high input impedance and a gain very close to 1.
The Circuit Schematic
The figure shows the full schematic for the constant-current electronic load. Below are the additional components used in the circuit:
- Pico W: manages the communication with Arduino Manager (and the user), measures the required quantities and handle all the system operations
- Thermistor: the MOSFET temperature is constantly monitored to avoid that the it overheats and damege. The NTC connected to the Pico W serves this purpose.
- Fan: To keep the power MOSFET cool, it is mounted on a heatsink, and a fan provides additional airflow. The fan operates when the MOSFET temperature rises above a user-defined threshold. A BS170 controls the Fan but any other similar MOSFET will do the work
- R4 and R5: These resistors form a voltage divider that allows the Pico's ADC (which accepts a maximum of 3.3 V) to measure the main voltage applied to the electronic load. We assume that the maximum voltage is around 30 V. For higher voltages, R4 and R5 must be recalculated.
Powering the Electronic Load
According to the schematic, two voltages are required to power the circuit:
- 9/12V to power the op-amp
- 5V to power the Pico W via the D1 as recommended in Raspberry Pi Pico W Datasheet (section 3.5).
The 5V can be easily obtained using a DC-DC converter (any inexpensive module should work). Alternatively, the Pico can be powered via a USB wall adapter or even the USB port of a computer.
Building the Electronic Load
There are many different ways to build the proposed electronic load: custom PCB, perfboard, veroboard, or a breadboard.
The breadboard is the easiest way to experiment with the circuit and adapt it to your requirements.
The picture provides a reference for building the circuit on the breadboard.
Arduino Manager - Firmware Overview
In the next steps, using the Arduino Manager's code generator, we will generate the firmware code for Pico W and adapt it as required.
In this section, we provide an overview of the structure of the generated code to make the following steps easier to understand.
The code for supporting Arduino Manager is mainly based on four functions:
- doWork - Replaces the classic Arduino loop() function. Some of your code goes here.
- processIncomingMessages - Receives information from the connected iOS/macOS device
- processOutgoingMessages - Sends information to the connected iOS/macOS device
- doSync - Sends status information to the iOS/macOS devices as soon as it connects
For more information:
- Read the previous Instructables starting from Control Arduino From Your IOS Device and Your Apple Watch
- Read the Arduino Manager documentation (Arduino Manager Documentation).
Configuring Arduino Manager
Now we are ready for:
- Configure Arduino Manager
- Use the Arduino Manager's Code Generator to create the firmware for Raspberry Pico
For this Instructable you need Arduino Manager version 23.2.0 or greater.
We start opening Arduino Manager and creating a new Widget List:
- Widget Lists from the left menu
- Click New
- Choose to store it in the cloud in order to share it between all your devices
- Enter the widget list's name: Constant Current Electronic Load
- Select the newly created Widget List
Then we have to create a connection to allow the Pico to connect to the Wi-Fi network.
- From the left menu, select Connections
- Click New Wi-Fi
- Enter the connection details:
- Name: Electronic Load Connection
- IP: 192.168.1.98 (use a value compatible with your Wi-Fi network configuration)
- Port: 100
Return to the Widget List:
- Select Widgets from the left menu
- Put the Widget List into edit mode using the Edit switch located at the bottom of the screen
- Click on the Wrench at the top right of the screen
- Select the Constant Current Electronic Load connection
We are now ready to add the widgets we need. Let’s start with the first one:
- Click on the plus button in one of the free spaces
- Select the Slider widget to set the desired current drawn by the Electronic Load
- Replace the ? with the name of the widget: Current
- Click to configure the widget
- Enter the maximum value for the current: 2
- Choose a color for the slider and click Ok
- Switch back from the edit mode.
Generate the Code for the First Widget
From the left menu, open the Code Generator to create a new project:
- Click New
- Enter the name of the new project: Constant Current Electronic Load
- Enter the project details:
- Board: Raspberry Pico W (in case you have the Pico 2 W, select it instead. Arduino Manager will automatically handle the project configuration.)
- Communication Device: Built in WiFi (in case you prefer to use Bluetooth, select it instead. Arduino Manager will automatically handle the project configuration.)
- Connection: Electronic Load Connection. (Some of the connection details are copied from the connection, others will be entered.)
- Netmask: 255.255.255.0 (use a value compatible with your network configuration.)
- Gateway: 192.168.1.1 (use a value compatible with your network configuration.)
- SSID: Enter the name of your WiFi network.
- Password: Enter the password used to authenticate to the WiFi network.
- Connection Callback: off
- Disconnection Callback: on
- Click Widget Pins to configure the pins used by each Gpio pin
- Select the Slider widget and enter the required details:
- Pin: 17 (according to the schematics the Pin used to set the current is Gpio 17)
- Value at Power On: 0
- Click Generate
Now the new project is created and available in your iCloud drive.
Some additional steps are now required to open the project in Visual Studio Code (we assume that you have already configured the development environment. If not, use the official documentation: Getting started with Raspberry Pi Pico-series Microcontrollers).
For the remaining part of this Instructable we assume that you are using the Debug Probe).
- Start Visual Studio Code
- Open the Raspberry Pi extension from the left side menu
- Select Import Project
- Select the project: iCloud Driver -> Arduino Manager -> Constant_Current_Electronic_Load
- Click Import
- Create a lib folder
- Right click on the lib folder and select Open in Integrated Terminal
- Enter the following commands:
- Click on Raspberry Pi Pico Extension on the left bar and select Configure CMake
- Ctrl + F5 to start the program.
Custom Code for the Current Widget
The generated code already set the value of the PWM pin automatically.
but the value is in the range: 0 - 2. We need to transform that range in the proper duty cycle.
- Update the slider callback as follows:
- Go to the top of the code and change the following section:
as follows:
- Then go to the bottom of the code and add a new function:
As already mentioned, this function calculates the duty cycle required to obtain a proper DC voltage and then set it on the GPIO PIN.
Finally, we turn off the current when Arduino Manager disconnects from the Pico with this simple change:
- Press Ctrl + F5 to compile and run the code.
First Test
- In Arduino Manager, select Widgets from the side menu to open the Constant Current Electronic Load.
- Connect Arduino Manager to the Pico by clicking the Connect button and wait until the LED at the bottom left of the screen turns on.
- Move the slider to a small current value, for example, 0.5 A.
- Using a multimeter, measure the control voltage (the voltage across Rf).
- If everything is working correctly, you should measure a voltage of approximately 0.5 V.
Reading the Control Voltage
Now we will measure the control voltage using one of the Pico’s Analog-to-Digital Converters (ADCs) and display this information in Arduino Manager.
Put the widget list in edit mode and and then follow the steps below:
- Add a Display widget
- Configure it as follows:
- Name: Current
- Variable: ctrl_voltage
- Unit of measure: [V]
- Number of Digits: 3
- Exit from the edit mode
- Go to Code Generator and select Widget Pins
- Select the Display widget and ente the following details:
- Pin: 26 (as per schematic)
- Read Value Every: 500ms
- Input Preprocess Function: yes
- Switch back from the edit mode.
- Click Generate to generate the code
- We need to convert the integer value read by the ADC to the voltage value with the formula:
where Vref is the reference voltage used by the ADC that in this case is the power voltage (3.3V).
- To make better measurements we need to perform more ADC readings and average them. This is the purpose of this function to add at the really bottom of the file:
- Go at the top of the code in the section Start your code (defines) and add these lines:
- Finally, change the preprocess_display_ControlVoltage function as follows
- Go to the widget list (menu widgets) and connect to the board
- Set a current (e.g. 0.5A)
- The Display widget should display a value very close to the set current
Control Voltage ADC Readings Calibration
You may have noticed that the ADC readings at 0V are not exactly 0. For that reason we need to calibrate each ADC channel.
We are going to use a two points calibration (see Calibrating Sensors).
First we need to make some changes to the code.
- Download the class Calibrator and copy it into your project folder.
- Change the Start your code (defines) as follows
- Modify the function preprocess_display_ControlVoltage as follows
- Upload and run the code
Now we have to identify the readings returned by the ADC to use for the calibration:
- Using a digital multimeter, read the 3.3V voltage at Pin 36 of the Pico.
- Modify the value of Vcc accordingly
- Connect the ADC pin to ground
- From Arduino Manager connect to the board
- Read the value measured by the ADC (let's suppose it is 0.014)
- Connect the ADC pin to 3.3V
- Read the value measured by the ADC (let's suppose it is 3.321)
- Modify the ctlr_voltage_calibrator definition using the value just read:
Downloads
Reading the MOSFET Temperature
As already mentioned, to avoid damaging the MOSFET, we continuously measure its temperature and display it in Arduino Manager. Later in the Instructable, we will automatically turn on the fan to keep the MOSFET as cool as possible.
To read the MOSFET temperature (via a thermistor) we initially configure Arduino Manager and calibrate the ADC.
Put the widget list in edit mode and and then follow the steps below:
- Add a Display widget
- Configure it as follows:
- Name: Temperature
- Variable: Temperature
- Unit of measure: ˚C
- Number of Digits: 2
- Exit from the edit mode
- Go to Code Generator and select Widget Pins
- Select the Display widget and ente the following details:
- Pin: 28 (as per schematic)
- Read Value Every: 500ms
- Input Preprocess Function: yes
- Switch back from the edit mode.
- Click Generate to generate the code
- Create a new calibrator
- Modify the function preprocess_display_Temperature:
- Run the code and using Arduino Manager identify the values for the ADC calibration like we in step 11. Use the read values for the calibrator (we assume they are the same of the other ADC channel)
Now that the voltage reading is correct, we have to convert it into temperature. This is done in two steps:
- From the voltage measurement, we calculate the resistance of the thermistor
- using the thermistor resistor, we use the Steinhart–Hart equation to calculate the temperature
You will find all the details in a past Instructable: How to Measure a Remote Temperature Using Pico W and Arduino Manager.
- In the Start your code (defines) section add:
- the include for the mathematical functions
- the coefficients for the Steinhart–Hart equation (provided in the thermistor datasheet). Enter the values specified for the thermistor you are using
- thermistor resistor value @ 25˚C
- the value of the shunt resistor
- two heather for the conversion functions.
- At the very bottom of the file add the following functions:
Finally, complete the function preprocess_display_Temperature:
Run the code, connect from Arduino Manager and the Temperature Display should display the temperature of the MOSFET.
Controlling the Fan
We could easily control the fan manually using a Switch widget in Arduino Manager, but we want to make everything automatic.
We can use the Code Generator to configure a digital GPIO to control the fan. The Code Generator will provide a variable that allows us to do this easily.
- Select Code Generator from the left menu
- Select Other Pins
- Click New Digital Pin
- Configure the new Pin as follows:
- Pin: 16 (as per schematic)
- Variable: fan
- Direction: Output
- Value at Power On: Low (fan is off when the Electronic Load starts)
- Switch back from the edit mode.
- Click Generate
The fan should operate when the MOSFET temperature rises above a threshold and should stop when the temperature falls below a threshold. The Thermostat widget helps us manage this behavior.
- Open the Widget list and switch to edit
- Add a Thermostat Widget
- Configure it as follows:
- Name: Temperature
- Variable: Temperature
- Low: 25
- High: 45
- Step: 5
- Select Code Generator from the left menu
- Switch back from the edit mode.
- Click Generate
Now we have available two variables:
- temperature_low
- temperature_high
that are automatically updated with the values set in Arduino Manager and a function thermostat_Temperature_check that can be used to react to the temperature getting either below or above the threshold changing the function as follows:
- Upload and run the code.
- From Arduino Manager connect to the Pico.
- On the Thermostat widget select L to configure the lower threshold and set it to 20.
- Select H to configure the higher threshold and set it to 40.
- Set the current to an high value such as 1.5A to make the MOSFET temperature rise quickly.
- As soon as the temperature rises above 40 ˚C the fan should switch on.
- Set the current to 0A.
- The MOSFET temperature start to fall and as soon as it drops below 20 ˚C the fan should switch off.
Now turn off the Pico and then turn it back on or upload the code again. Then without changing anything on the Threshold widget set the current to 1.5A and wait for the temperature to rise above 40 ˚C.
The fan doesn't start. This because the previously set threshold have been lost at the Pico power off.
We need to store the thresholds in flash memory so they are preserved across power cycles.
- Download the flash_storage.h and flash_storage.cpp and copy them to your project folder
- Open the file CMakeLists.txt and add the flash_storage.ccp file
- Update the Start your code (defines) section as follows:
- Locate the /* Start your code (setup) */ section and add the following lines
At startup the structure cfg is restored from the flash. If this is not possible because it was never saved before, each field is properly initialized.
Ignore shutoff_temp for now. It will be used later.
- The function thermostat_Temperature_callback is called each time Arduino Manager sets the Thermostat widget's low or high threshold. We have to update it in order to save the threshold values in the flash memory.
The Threshold Widget
Now that we measure the temperature, we can prevent the MOSFET from overheating by setting a threshold above which the electronic load automatically turns off the drawn current.
- Open Arduino Manager, using the side menu open the Widget list and switch to edit
- Add a Threshold Widget
- Configure it as follows:
- Name: Max Temperature
- Variable: Max_temp
- Min: 60
- Max: 110
- Step: 2
- Value: 60
- Exit from the edit mode
- Go to Code Generator and click Generate
- This threshold has to be saved in the Flash memory as well
- The function threshold_MaxTemperature_callback is called each time Arduino Manager sets the Threshold widget's value. We have to update it in order to save the threshold value in the flash memory.
- Update the thermostat_Temperature_check adding the following lines:
- To test that it works as expected, proceed with the following steps:
- From Arduino Manager set the current to the maximum level: 2A
- The MOSFET temperature should rise very quickly until the fan starts
- If the fan and the heatsink are not effective enough to stabilize the MOSFET temperature, it will continue to rise
- As soon as the temperature reaches the threshold set by the Max Temperature widget, the current is automatically reset to 0. The fan keeps running to help cool the MOSFET.
Led Widget & Sound Alarm Widget
We can now add two useful widgets:
- LED widget: to see when the fan is operating
- Sound Alarm widget: to receive an audible alert when the temperature is close to the maximum allowed value
Follow these steps:
- Open the Widget list from the side menu and switch to edit mode
- Add a LED widget and configure it as follows:
- Name: Fan
- Variable: Fan
- Color: whatever you like
- Add a Sound Alarm widget and configure as follows:
- Name: Temperature Alert
- Variable: Temperature
- Sound: whatever you like
- Operator: >
- Threshold: 55
- Switch back from the edit mode.
- Open the Code Generator and click Generate
- Upload and run the code
Now the LED will turn on when the fan is operating, and as soon as the temperature reaches 55 °C, a sound will be played.
Power Calculation
May be useful to know what is the power drawn by the electronic load. The power is calculated using this formula:
Power = Vin * Irf
We know that:
Control Voltage
IRf = -------------------
Rf
Arduino Manager already receives the value of the (measured) Control Voltage, we just need to calculate the current. Since in our case Rf is 1Ω this calculation is unnecessary but but it serves as a good demonstration of Arduino Manager's capabilities.
- Open the widget list and switch to edit.
- Add a Display Widget and configure as follows
- Name: Current
- Variable: Ir
- Unit of Measure: [A]
- Number of Digits: 2
- Calculated Widget: Yes
This widget does not receive any information from the board; therefore, the Code Generator will not generate any code for it. However, Arduino Manager (more precisely, the Calculator widget) will keep it updated by calculating its value.
- Add a Calculator Widget and add a new formula
- Variable: Ir
- Formula: ctrl_voltage / 1
- Switch back from the edit mode.
We now have to measure Vin. We proceed as we did for measuring the control voltage:
- Open the widget list and switch to edit.
- Add a Display Widget and configure it as follows:
- Name: Vin
- Variable: Vin
- Unit of Measure: [V]
- Number of Digits: 2
- Calculated Widget: No
- Switch back from the edit mode.
- Select Code Generator
- Select Widget Pins
- Select Display widget for Vin
- Configure the Pin as follows:
- Pin: 27
- Read Value Every: 500ms
- Input Preprocess function: Yes
- Click Generate
The generated code has to be customized as follows:
- In the section Start your code (defines) add
The calibrator for this ADC channel has been configured like the other channels, its better define the specific values for the channel using the procedure previously described.
- Finally, update the preprocess_display_Vin:
- We now need a (calculated) Display widget to display the Power.
- Name: Power
- Variable: Power
- Unit of Measure: [W]
- Number of Digits: 2
- Calculated Widget: Yes
- The last configuration is to add a new calculation formula to the Calculator widget:
- Variable: Power
- Formula: Vin * Ir
Current Values Sequence
For testing your device, you may need to apply a pre-defined sequence of currents to the load. This can be easily done in Arduino Manager by adding a Value Scheduler widget.
- Open the widget list and switch to edit.
- Add a Value Scheduler widget and configure it as follows:
- Name: Values
- Variable: Current
- Start on Connection: No
- Repeat Sequence: No
- Time Slot 1: 2000ms / 0.5
- Time Slot 2: 1500ms / 1
- Time Slot 3: 1000ms / 2
- Time Slot 4: 100ms / 0
- Now click on Start. The sequence will begin, and the electronic load will draw the pre-defined currents from the load
Conclusion
It has been a long journey, but in the end we created an inexpensive yet powerful tool—the Electronic Load—that can be effortlessly controlled from an iOS or macOS device.
Arduino Manager proves to be a compelling alternative to traditional device GUIs, eliminating the need for physical displays, endless lines of code, and hours of development just to achieve a polished interface.
With Arduino Manager, you can design, control, and monitor even complex systems in a matter of minutes.