arduinohardwarebbc-microbit

Trying to programmatically disconnect microbit Pin


I am trying to set up a lab for students to investigate capacitance via measuring voltage drop over a capacitor using a microbit. I have a microbit hooked up to a breadboard with a capacitor hooked up in parallel with an LED/resistor. The idea is to kill power on the pin powering the circuit and have students watch the LED slowly dim, while the microbit records the capacitor voltage every half second. I am having trouble programmatically disconnecting pin 1 on the microbit. I have connected the same circuit to an arduino and successfully done this, but have been unable to translate the code to microbit. If I just use digitalWrite to set the voltage to 0, that instantly drains my capacitor, while if I use setPull, the light never dims and power is never killed. I have an image of my circuit setup and all of my code in python (which is where the issue lies).

time = 0
# pins.set_pull(DigitalPin.P1, PinPullMode.PULL_DOWN)
pins.digital_write_pin(DigitalPin.P1, 0)
pause(2000)
# pins.set_pull(DigitalPin.P1, PinPullMode.PULL_UP)
pins.digital_write_pin(DigitalPin.P1, 1)
pause(2000)

def on_forever():
    global time
    pins.set_pull(AnalogPin.P1, PinPullMode.PULL_NONE)
    # pins.digitalWritePin(DigitalPin.P1, 0)
    time = 0
    for index in range(15):
        serial.write_number(6)
        basic.show_number(pins.analog_read_pin(AnalogPin.P0))        
        datalogger.log(datalogger.create_cv("A", time))
        datalogger.log(datalogger.create_cv("B", pins.analog_read_pin(AnalogPin.P0)))
        time += 500
        pause(500)
basic.forever(on_forever)

The arduino code that successfully does this task is below:

void setup() {
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  pinMode(A1, INPUT);
  digitalWrite(8, HIGH);
  delay(2000);
}
bool first = true;
void loop() {
  int data[20]  = {0};
  pinMode(8, INPUT);
  if (first)
    for (int i = 0; i < 15; i++) {
      data[i] = analogRead(A1);
      Serial.print(data[i]/1023. * 5.);
      Serial.print(",");
      delay(500);
    }
  first = false;
}

Circuit Layout


Solution

  • I just needed to do a readDigital() on pin 1 in order to switch it to an input