MojoScale Studio Docs
API Reference

lm358

LM358 microphone sound sensor module.

Studio Docs Sensors & I/O

import lm358

The lm358 module reads common 3-pin LM358 sound sensor boards with VCC, GND, and SIG/S pins. These boards use an electret microphone and an LM358 amplifier; the SIG pin is an analog voltage, so the native module samples an ADC-capable GPIO and reports relative sound amplitude. Use this module for clap/knock detection, relative loudness, and simple noise presence checks. It does not measure calibrated decibels.

Quick example

import lm358
import system

# ESP32 with WiFi: use ADC1 pins such as GPIO32-GPIO39.
# ESP32-S3 with WiFi: use ADC1 pins such as GPIO1-GPIO10.
sound = lm358.LM358(pin=34, threshold=0.08)

def check_sound():
    reading = sound.read()
    if reading["ok"] and reading["loud"]:
        print("sound detected", reading["level"])

system.schedule("sound", 100, 100, -1, check_sound)

importlm358

LM358 microphone sound sensor module.

The lm358 module reads common 3-pin LM358 sound sensor boards with VCC, GND, and SIG/S pins. These boards use an electret microphone and an LM358 amplifier; the SIG pin is an analog voltage, so the native module samples an ADC-capable GPIO and reports relative sound amplitude. Use this module for clap/knock detection, relative loudness, and simple noise presence checks. It does not measure calibrated decibels.

API 19 available

lm358.LM358(pin: int, samples: int = None, threshold: float = None, attenuation: int = None, delay_us: int = None) -> lm358_sensor

Create an LM358 sound sensor object.

The returned object reads one ADC-backed LM358 sound board. If a newer LM358 object is created, older objects fail clearly instead of reading from a replaced ADC configuration.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes ADC-capable GPIO connected to the LM358 SIG/S output. On ESP32-class boards, ADC2 pins can time out while WiFi is active. Prefer ADC1 pins for scripts that also use WiFi.
samples int positional or keyword No Optional number of ADC samples per window. Defaults to 64.
threshold float positional or keyword No Optional normalized loud threshold from 0.0 through 1.0. Defaults to 0.08.
attenuation int positional or keyword No Optional ADC attenuation in dB. Use 0, 2, 6, or 12. Defaults to 12.
delay_us int positional or keyword No Optional microseconds between samples. Defaults to 200.
Returns

lm358_sensor lm358_sensor object.

lm358.LM358().read(samples=None, delay_us=None) -> dict

Read a sound window with raw, level, and loud fields.

Parameters
Name Type Pass as Required Description
samples int positional or keyword No Optional value. Defaults to None.
delay_us int positional or keyword No Optional value. Defaults to None.

lm358.LM358().raw() -> int

Read one raw ADC sample.

lm358.LM358().level(samples=None, delay_us=None) -> float

Read normalized relative sound level.

Parameters
Name Type Pass as Required Description
samples int positional or keyword No Optional value. Defaults to None.
delay_us int positional or keyword No Optional value. Defaults to None.

lm358.LM358().loud(samples=None, delay_us=None) -> bool

Return whether level crosses the threshold.

Parameters
Name Type Pass as Required Description
samples int positional or keyword No Optional value. Defaults to None.
delay_us int positional or keyword No Optional value. Defaults to None.

lm358.LM358().calibrate(samples=128, multiplier=3.0, floor=0.02, delay_us=None) -> dict

Set threshold from the quiet noise floor.

Parameters
Name Type Pass as Required Description
samples int positional or keyword No Optional value. Defaults to 128.
multiplier float positional or keyword No Optional value. Defaults to 3.0.
floor float positional or keyword No Optional value. Defaults to 0.02.
delay_us int positional or keyword No Optional value. Defaults to None.

lm358.LM358().threshold(value=None) -> float

Get or set the normalized loud threshold.

Parameters
Name Type Pass as Required Description
value any positional or keyword No Optional value. Defaults to None.

lm358.LM358().info() -> dict

Return ADC pin, threshold, sample window, and last error state.

lm358.LM358().reset() -> bool

Release ADC state.

lm358.LM358().close() -> bool

Release ADC state and close this object.

lm358.begin(pin: int, samples: int = None, threshold: float = None, attenuation: int = None, delay_us: int = None) -> bool

Initialize an LM358 sound sensor on an ADC-capable pin.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes ADC-capable GPIO connected to the LM358 SIG/S output. On ESP32-class boards, ADC2 pins can time out while WiFi is active. Prefer ADC1 pins for scripts that also use WiFi.
samples int positional or keyword No Optional number of ADC samples per window. Defaults to 64.
threshold float positional or keyword No Optional normalized loud threshold from 0.0 through 1.0. Defaults to 0.08.
attenuation int positional or keyword No Optional ADC attenuation in dB. Use 0, 2, 6, or 12. Defaults to 12.
delay_us int positional or keyword No Optional microseconds between samples. Defaults to 200.
Returns

bool True when the pin maps to an ADC channel and the reader is configured.

lm358.read(samples: int = None, delay_us: int = None) -> dict

Read a short sound window.

Parameters
Name Type Pass as Required Description
samples int positional or keyword No Optional number of ADC samples for this read only.
delay_us int positional or keyword No Optional microseconds between samples for this read only.
Returns

dict Dict with ``ok``. When ``ok`` is true, it includes ``pin``, latest ``raw`` sample, ``min``, ``max``, ``amplitude`` peak-to-peak count, average raw value, normalized ``level`` from 0.0 through 1.0, ``loud`` bool, ``threshold``, ``samples``, and ``delay_us``. When ``ok`` is false, it includes ``error`` and ``hint``.

lm358.raw() -> int

Read one raw ADC sample.

Returns

int Integer raw ADC count from the configured pin.

lm358.level(samples: int = None, delay_us: int = None) -> float

Read normalized relative sound level.

Parameters
Name Type Pass as Required Description
samples int positional or keyword No Optional number of ADC samples for this read only.
delay_us int positional or keyword No Optional microseconds between samples for this read only.
Returns

float Float level from 0.0 through 1.0 based on peak-to-peak ADC movement.

lm358.loud(samples: int = None, delay_us: int = None) -> bool

Return whether current sound level crosses the configured threshold.

Parameters
Name Type Pass as Required Description
samples int positional or keyword No Optional number of ADC samples for this read only.
delay_us int positional or keyword No Optional microseconds between samples for this read only.
Returns

bool True when the current window level is greater than or equal to the configured threshold.

lm358.calibrate(samples: int = None, multiplier: float = None, floor: float = None, delay_us: int = None) -> dict

Set threshold from the current quiet noise floor.

Keep the sensor in its normal quiet environment while calling this. The module reads one window, multiplies the observed level, and stores the result as the new loud threshold.

Parameters
Name Type Pass as Required Description
samples int positional or keyword No Optional number of ADC samples to use. Defaults to 128.
multiplier float positional or keyword No Optional multiplier applied to measured quiet level. Defaults to 3.0.
floor float positional or keyword No Optional minimum threshold. Defaults to 0.02.
delay_us int positional or keyword No Optional microseconds between samples.
Returns

dict Dict with ``ok``, measured ``noise_level``, stored ``threshold``, ``multiplier``, ``floor``, ``samples``, and ``amplitude``.

lm358.threshold(value: float = None) -> float

Get or set the normalized loud threshold.

Parameters
Name Type Pass as Required Description
value float positional or keyword No Optional threshold from 0.0 through 1.0.
Returns

float Float current threshold.

lm358.info() -> dict

Return LM358 module state.

Returns

dict Dict with initialization state, pin, sample window, threshold, ADC unit, ADC channel, attenuation, last error, and hint.

lm358.reset() -> bool

Release ADC state and clear the configured pin.

Returns

bool True when module state is cleared.