Arduino Tutorials

Custom Chips with Live Controls in the Browser Simulator

If you’ve ever needed a part that wasn’t in the simulator’s catalog, you know the pain of working around it. The custom chip feature has been the escape hatch for a while — you write C, it compiles to WebAssembly, and it behaves like real silicon. The catch was that the chip lived in a modal dialog, separate from your main sketch, and you couldn’t change its behavior while the simulation ran. That’s changed. Now custom chips are first-class citizens in the file explorer, and you can add live controls — sliders that update sensor values in real time, no recompiling.

This matters because a sensor that just sits there isn’t much use for testing. You want to simulate a CO2 level rising, or temperature climbing, and see how your firmware responds. The new live controls let you do that, and the chip’s DAC output can feed into your board’s ADC, so analogRead() sees the changes immediately.

Wiring It Up

Let’s build a simple CO2 sensor with an analog output. You’ll need:

  • An Arduino Uno (or ESP32, the process is the same)
  • A custom chip placed on the breadboard
  • One wire from the chip’s DAC0 pin to the Arduino’s A0 pin

In the simulator, click the custom chip to open its settings, then connect the pins. The chip’s manifest defines the DAC pin as dac0. The Arduino reads that voltage on A0. That’s the entire circuit — no external components needed.

The Sketch

Here’s the Arduino sketch that reads the sensor and prints the CO2 value to the serial monitor:

// Read analog value from A0, convert to CO2 ppm, print to serial.

const int sensorPin = A0;  // DAC output from custom chip

void setup() {
  Serial.begin(9600);
}

void loop() {
  int raw = analogRead(sensorPin);
  // Map 0-1023 to 400-5000 ppm (matches slider range)
  int ppm = map(raw, 0, 1023, 400, 5000);
  Serial.print("CO2: ");
  Serial.print(ppm);
  Serial.println(" ppm");
  delay(500);
}

How It Works

The custom chip is defined by two files: chip.c and chip.json. The manifest declares the chip’s pins and any controls. For our CO2 sensor, the manifest looks like this:

{
  "name": "CO2 Sensor",
  "pins": [
    { "id": "dac0", "type": "dac" }
  ],
  "controls": [
    { "id": "ppm", "label": "CO2 (ppm)", "type": "range",
      "min": 400, "max": 5000, "step": 10, "unit": "ppm" }
  ]
}

The chip.c file reads the control attribute and writes the corresponding voltage to the DAC pin. When you drag the slider, the attribute updates, and the chip’s DAC output changes. The simulator routes that to the Arduino’s ADC, so analogRead(A0) returns a value proportional to the slider position.

The key function is vx_attr_read(), which reads the current value of a control attribute. The chip code might look like:

// In chip.c
#include "velxio-chip.h"

void chip_init() {
  // Set DAC output to 0V initially
  vx_dac_write(0, 0);
}

void chip_update() {
  int ppm = vx_attr_read("ppm");
  // Convert ppm to voltage (0-5V)
  float voltage = (ppm - 400.0) / (5000.0 - 400.0) * 5.0;
  vx_dac_write(0, voltage);
}

The simulator calls chip_update() every simulation tick, so the output stays in sync with the slider.

Honest Notes: What the Simulator Won’t Tell You

This is a simplified model. Real analog sensors have noise, drift, and nonlinearity — this chip gives you a clean, linear output. The DAC resolution is also limited; the simulator uses a fixed number of bits, so you won’t see the same granularity as a real 12-bit DAC. And the mapping from slider to voltage is purely linear; real sensors might have a different curve.

The simulator also doesn’t model electrical characteristics like output impedance or loading. If you connect the chip’s DAC to a low-impedance input, you might see a voltage drop in real life, but not here. That’s fine for logic testing, but don’t expect it to match a bench measurement.

One more gotcha: the map() function in the sketch assumes the ADC is 10-bit (0-1023). If you use an ESP32, the ADC is 12-bit, so you’d need to adjust the mapping. The simulator defaults to 10-bit for Arduino, but it’s something to watch.

When It Doesn’t Work

  • Symptom: Slider doesn’t change the analog reading. Cause: The chip’s DAC pin isn’t connected to the Arduino’s A0 pin. Check the wiring.
  • Symptom: Slider doesn’t appear when you click the chip. Cause: The manifest might be missing the controls array, or the id doesn’t match an attribute. Ensure the JSON is valid and the control id matches an attribute name.
  • Symptom: Chip doesn’t compile. Cause: Syntax error in chip.c, or the manifest has an invalid pin type. Check the simulator’s compile output.
  • Symptom: Analog reading jumps between 0 and 1023 only. Cause: The chip’s DAC output might not be updating because chip_update() isn’t being called, or the control attribute isn’t being read correctly.

Next Steps

Try building an I2C environment sensor with temperature and humidity sliders. The manifest can declare multiple controls, and the chip can implement an I2C register map. Or add a button control to simulate a digital input. You can also save your chip to My Chips (pro feature) to reuse it across projects.

Open the simulator and give it a shot.

Leave a Reply

Your email address will not be published. Required fields are marked *