# Plasma 2040 CircuitPython current sense

**URL:** https://forums.pimoroni.com/t/plasma-2040-circuitpython-current-sense/17927
**Category:** Support
**Created:** [September 20, 2021, 12:45pm UTC](https://forums.pimoroni.com/t/plasma-2040-circuitpython-current-sense/17927 "2021-09-20T12:45:19Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![simonbooth](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@simonbooth](https://forums.pimoroni.com/u/simonbooth)
#### Post date: [September 20, 2021, 12:45pm UTC](https://forums.pimoroni.com/t/plasma-2040-circuitpython-current-sense/17927/1 "2021-09-20T12:45:19Z")

</div>

Hi, I’ve just received a Plasma 2040. I’m new to CircuitPython, and Python in general, but have managed to get some animation effects working with a WS2812 strip.

Can anyone give me a hint on how to read the current draw using CircuitPython? There is an example for MicroPython but I can’t seem to adapt it for CircuitPython. Looking at the source code there appears to be board.CURRENT\_SENSE available, but i don’t understand how to use it.

Any help would be greatly apprectiated.

---

<div class="post-metadata">

### Author: ![hel](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/hel/32/6075_2.png) [@hel](https://forums.pimoroni.com/u/hel)
#### Post date: [September 20, 2021, 5:14pm UTC](https://forums.pimoroni.com/t/plasma-2040-circuitpython-current-sense/17927/2 "2021-09-20T17:14:03Z")

</div>

Ahoyhoy!

Here’s some CircuitPython code that should do the same thing as the [MicroPython rainbow.py example](https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/plasma2040/rainbow.py), including reading the buttons, driving the RGB LED and reading the current :)

```auto
import time
import board
import digitalio
from analogio import AnalogIn
import adafruit_rgbled
import busio
import neopixel
import adafruit_dotstar as dotstar
import math

# Press "B" to speed up the LED cycling effect.
# Press "A" to slow it down again.
# Press "Boot" to reset the speed back to default.

# Set how many LEDs you have
NUM_LEDS = 30

# The speed that the LEDs will start cycling at
DEFAULT_SPEED = 10

# How many times the LEDs will be updated per second
UPDATES = 60

# How bright the LEDs will be (between 0.0 and 1.0)
BRIGHTNESS = 0.5

# Pick *one* LED type by uncommenting the relevant line below:

# APA102 / DotStar™ LEDs
# led_strip = dotstar.DotStar(board.CLK, board.DATA, NUM_LEDS, brightness=BRIGHTNESS)

# WS2812 / NeoPixel™ LEDs
led_strip = neopixel.NeoPixel(board.DATA, NUM_LEDS, brightness=BRIGHTNESS, auto_write=False)

user_sw = digitalio.DigitalInOut(board.USER_SW)
user_sw.direction = digitalio.Direction.INPUT
user_sw.pull = digitalio.Pull.UP

sw_a = digitalio.DigitalInOut(board.SW_A)
sw_a.direction = digitalio.Direction.INPUT
sw_a.pull = digitalio.Pull.UP

sw_b = digitalio.DigitalInOut(board.SW_B)
sw_b.direction = digitalio.Direction.INPUT
sw_b.pull = digitalio.Pull.UP

led = adafruit_rgbled.RGBLED(board.LED_R, board.LED_G, board.LED_B, invert_pwm = True)

sense = AnalogIn(board.CURRENT_SENSE)

# Constants used for current conversion
ADC_GAIN = 50
SHUNT_RESISTOR = 0.015 # Yes, this is 0.015 Ohm

def get_voltage(pin):
    return (pin.value * 3.3) / 65536
    
def get_current(pin):
    return get_voltage(pin) / (ADC_GAIN * SHUNT_RESISTOR)

def hsv_to_rgb(h, s, v):
	# All inputs are from 0.0 to 1.0
    i = math.floor(h * 6.0)
    f = h * 6.0 - i
    v *= 255.0
    p = v * (1.0 - s)
    q = v * (1.0 - f * s)
    t = v * (1.0 - (1.0 - f) * s)

    zone = int(i) % 6
    if zone == 0:
        return (v, t, p)
    if zone == 1:
	    return (q, v, p)
    if zone == 2:
        return (p, v, t)
    if zone == 3:
	    return (p, q, v)
    if zone == 4:
	    return (t, p, v)
    if zone == 5:
		return (v, p, q)
    return (0, 0, 0)

def button_read(button):
	return not button.value

speed = DEFAULT_SPEED
offset = 0.0

count = 0
# Make rainbows
while True:
    sw = not user_sw.value
    a = not sw_a.value
    b = not sw_b.value
	
    if sw:
		speed = DEFAULT_SPEED
    else:
        if a:
            speed -= 1
        if b:
            speed += 1
			
	speed = min(255, max(1, speed))
	
	offset += float(speed) / 2000.0

    for i in range(NUM_LEDS):
        hue = float(i) / NUM_LEDS
        led_strip[i] = hsv_to_rgb(hue + offset, 1.0, 1.0)
	led_strip.show()
	
    led.color = (speed, 0, 255 - speed)

    count += 1
    if count >= UPDATES:
        # Display the current value once every second
        print("Current =", get_current(sense), "A")
        count = 0

    time.sleep(1.0 / UPDATES)

```

---

<div class="post-metadata">

### Author: ![simonbooth](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@simonbooth](https://forums.pimoroni.com/u/simonbooth)
#### Post date: [September 20, 2021, 8:21pm UTC](https://forums.pimoroni.com/t/plasma-2040-circuitpython-current-sense/17927/3 "2021-09-20T20:21:23Z")

</div>

That’s fantastic, exactly what I was after :)

Thank you for such a quick reply!

Simon.

---

<div class="post-metadata">

### Author: ![Joshy-J](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/joshy-j/32/8184_2.png) [@Joshy-J](https://forums.pimoroni.com/u/Joshy-J)
#### Post date: [September 27, 2021, 7:04pm UTC](https://forums.pimoroni.com/t/plasma-2040-circuitpython-current-sense/17927/4 "2021-09-27T19:04:04Z")

</div>

Where did you buy this from?!! They’ve been sold out for ages!
