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.
lm358.LM358(pin: int, samples: int = None, threshold: float = None, attenuation: int = None, delay_us: int = None) -> lm358_sensor
lm358.LM358(pin: int, samples: int = None, threshold: float = None, attenuation: int = None, delay_us: int = None) -> lm358_sensorCreate 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.
| 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. |
lm358_sensor lm358_sensor object.
lm358.LM358().read(samples=None, delay_us=None) -> dict
lm358.LM358().read(samples=None, delay_us=None) -> dictRead a sound window with raw, level, and loud fields.
| 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
lm358.LM358().raw() -> intRead one raw ADC sample.
lm358.LM358().level(samples=None, delay_us=None) -> float
lm358.LM358().level(samples=None, delay_us=None) -> floatRead normalized relative sound level.
| 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
lm358.LM358().loud(samples=None, delay_us=None) -> boolReturn whether level crosses the threshold.
| 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
lm358.LM358().calibrate(samples=128, multiplier=3.0, floor=0.02, delay_us=None) -> dictSet threshold from the quiet noise floor.
| 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
lm358.LM358().threshold(value=None) -> floatGet or set the normalized loud threshold.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
value |
any |
positional or keyword | No | Optional value. Defaults to None. |
lm358.LM358().info() -> dict
lm358.LM358().info() -> dictReturn ADC pin, threshold, sample window, and last error state.
lm358.LM358().reset() -> bool
lm358.LM358().reset() -> boolRelease ADC state.
lm358.LM358().close() -> bool
lm358.LM358().close() -> boolRelease ADC state and close this object.
lm358.begin(pin: int, samples: int = None, threshold: float = None, attenuation: int = None, delay_us: int = None) -> bool
lm358.begin(pin: int, samples: int = None, threshold: float = None, attenuation: int = None, delay_us: int = None) -> boolInitialize an LM358 sound sensor on an ADC-capable pin.
| 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. |
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
lm358.read(samples: int = None, delay_us: int = None) -> dictRead a short sound window.
| 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. |
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
lm358.raw() -> intRead one raw ADC sample.
int Integer raw ADC count from the configured pin.
lm358.level(samples: int = None, delay_us: int = None) -> float
lm358.level(samples: int = None, delay_us: int = None) -> floatRead normalized relative sound level.
| 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. |
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
lm358.loud(samples: int = None, delay_us: int = None) -> boolReturn whether current sound level crosses the configured threshold.
| 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. |
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
lm358.calibrate(samples: int = None, multiplier: float = None, floor: float = None, delay_us: int = None) -> dictSet 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.
| 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. |
dict Dict with ``ok``, measured ``noise_level``, stored ``threshold``, ``multiplier``, ``floor``, ``samples``, and ``amplitude``.
lm358.threshold(value: float = None) -> float
lm358.threshold(value: float = None) -> floatGet or set the normalized loud threshold.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
value |
float |
positional or keyword | No | Optional threshold from 0.0 through 1.0. |
float Float current threshold.
lm358.info() -> dict
lm358.info() -> dictReturn LM358 module state.
dict Dict with initialization state, pin, sample window, threshold, ADC unit, ADC channel, attenuation, last error, and hint.
lm358.reset() -> bool
lm358.reset() -> boolRelease ADC state and clear the configured pin.
bool True when module state is cleared.