Skip to content

Instantly share code, notes, and snippets.

@todbot
Created February 9, 2023 20:36
Show Gist options
  • Save todbot/58bcf7ea3a85aede3f951f8176e3aad5 to your computer and use it in GitHub Desktop.
Save todbot/58bcf7ea3a85aede3f951f8176e3aad5 to your computer and use it in GitHub Desktop.
Play with simplex noise in CircuitPython
# noise_square_code.py -- playing with simplex noise in CircuitPython
# 9 Feb 2023 - @todbot / Tod Kurt
# https://github.com/todbot/CircuitPython_Noise
# Uses Lolin ESP32-S2 Mini and 8x8 LED shield
# https://circuitpython.org/board/lolin_s2_mini/
#
import time
import board
import neopixel
import rainbowio
import noise
leds = neopixel.NeoPixel(board.IO16, 64, brightness=0.1, pixel_order=neopixel.RGB, auto_write=False)
leds.fill(0xff00ff) # say hello
noise_scale = 0.1
noise_x = 0
noise_y = 0
while True:
for i in range(8): # for each pixel row
for j in range(8): # for each pixel column
# get a noise value in 2D noise space
n = noise.noise( noise_x + noise_scale*i, noise_y + noise_scale*j )
c = int((n+1) * 255) # scale it from -1-1 -> 0-255
leds[j*8+i] = rainbowio.colorwheel(c) # convert to hue
leds.show()
noise_y += 0.01 # slide down in noise space
time.sleep(0.01)
@todbot
Copy link
Author

todbot commented Feb 9, 2023

Another example from the repo: https://github.com/todbot/CircuitPython_Noise

import time
from noise import noise

i = 0
while True:
    n = noise(0.02 * i)  # get a 1D noise value
    i += 1
    # print a random terrain with asterisks
    print(" " * int(max(n + 1, 0) * 40), "*")
    time.sleep(0.01)

which makes a really nice terrain-like output to the terminal

simplex_noise_terminal_demo.mp4

@pagong
Copy link

pagong commented Mar 2, 2025

Want to use this demo on larger NeoPixel displays (like 16x16 or 32x32)?
Then have a look at this: (which uses CPy9 and a WaveShare ESP32-S3-Zero)
https://github.com/pagong/CircuitPython_NeoMatrix/tree/main/examples/snoise

@todbot
Copy link
Author

todbot commented Mar 2, 2025

That's neat @pagong ! And thanks for porting the NeoMatrix library!

@pagong
Copy link

pagong commented Mar 3, 2025

Just for matter of completeness:
Tod's formula for mapping from [-1, +1] to [0, 255] does actually map to [0, 510], which looks a bit odd.
That's why I'm using this formula instead:

# Original:
# c = int((n+1) * 255)           # scale it from -1 - +1 -> 0 - 510
# Bugfix:
c = int((n+1.0) * 127.5)         # scale it from -1 - +1 -> 0 - 255

@todbot
Copy link
Author

todbot commented Mar 3, 2025

hahah oops, yep. Thanks @pagong!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment