You should start by organising your code better. Try to avoid global variables and group together code in functions. In addition, you should learn about list comprehensions. They make initializing your signals way easier. import RPi.GPIO as GPIO def twos_comp(i): ... def gpio_setup(inputs, outputs): GPIO.setmode(GPIO.BCM) for pin in inputs: GPIO.setup(pin, GPIO.IN) for pin in outputs: GPIO.setup(pin, GPIO.OUT) def main(): signals = [1 if i % 2 else -1 for i in range(26)] bit_signals = [twos_comp(i) for i in signals] for i in bit_signals: GPIO.wait_for_edge(5, GPIO.RISING) for counter, k in enumerate(str(i), 21): GPIO.output(counter, k) #k will be 0 or 1 which is accepted input if __name__ == "__main__": gpio_setup([5], range(21, 29)) main() GPIO.cleanup() And finally, there are better ways to do twos complement. A nice one is for example [here][1]. [1]: https://stackoverflow.com/a/37075643