Skip to content

Commit 92084ed

Browse files
committed
Update reference.md
1 parent 1ad8455 commit 92084ed

File tree

1 file changed

+65
-29
lines changed

1 file changed

+65
-29
lines changed

content/micropython/01.basics/08.reference/reference.md

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ For example:
1515
- `digitalWrite(pin, HIGH)` (Arduino/C++)
1616
- `pin.value(1)` (MicroPython)
1717

18-
The entries are named exactly the same as in the language reference, with the MicroPython syntax, description and code example provided for each entry.
18+
The entries are named exactly the same as in the language reference, with the MicroPython syntax, description and code example provided for each entry. It is however important to understand that any given board may not be fully compatible with this reference, as some implementations vary between board/architecture. For example, using PWM on a STM32 board will differ from using it on an ESP32 board.
1919

2020
***Note that several entries in the original [Language Reference](https://www.arduino.cc/reference/en/) are directly taken from the C++ reference. In the same fashion, many functions located in this reference are taken from the Python reference, and are not MicroPython specific.***
2121

@@ -30,7 +30,7 @@ To access digital GPIOs using MicroPython, we use the `Pin` module. To define a
3030
- `input_pullup = Pin(pin, Pin.IN, Pin.PULL_UP` (define as input pull up)
3131
- `input_pulldown = Pin(pin, Pin.IN, Pin.PULL_DOWN` (define as input pull down)
3232

33-
Parameters:
33+
**Parameters:**
3434
- `pin` - pin we want to set
3535
- `type` - define as input or output
3636
- `pullmode` - define as pull up or pull down mode (optional).
@@ -363,24 +363,35 @@ print("Rigthmost bit: ", rightmost_bit)
363363

364364
## Advanced I/O
365365

366-
### noTone()
366+
### tone() / noTone()
367367

368-
Stops generating a tone on a specified pin by deinitializing the PWM (Pulse Width Modulation) associated with the given pin.
368+
- `tone(pin,frequency,volume,duration)`
369+
- `noTone(pin,duration)`
369370

370-
**Parameters:**
371-
- `pin`: The pin number to stop generating the tone.
372-
373-
**Returns:**
374-
- Nothing
371+
To use the popular `tone()` and `noTone()` functions, we need to define them as functions as they currently are not implemented.
375372

376373
**Example:**
377374

378375
```python
379-
import machine
376+
from machine import Pin, PWM
377+
import time
378+
379+
def tone(pin,frequency,volume,duration=None):
380+
speaker = PWM(Pin(pin))
381+
speaker.freq(frequency)
382+
speaker.duty_u16(volume)
383+
if duration is not None:
384+
time.sleep(duration)
385+
386+
def noTone(pin,duration=None):
387+
speaker = PWM(Pin(pin))
388+
speaker.deinit()
389+
if duration is not None:
390+
time.sleep(duration)
380391

381-
def noTone(pin):
382-
pwm = machine.PWM(machine.Pin(pin))
383-
pwm.deinit()
392+
while True:
393+
tone(5,500,1000,1)
394+
noTone(5,1)
384395
```
385396

386397
### pulseIn()
@@ -427,95 +438,120 @@ def shiftOut(dataPin, clockPin, bitOrder=machine.MSBFIRST, value):
427438
machine.Pin(clockPin).value(0)
428439
```
429440

430-
### tone()
431-
432-
**Example:**
433-
434-
```python
435-
import machine
436-
437-
def tone(pin, frequency, duration=None):
438-
pwm = machine.PWM(machine.Pin(pin))
439-
pwm.freq(frequency)
440-
if duration is not None:
441-
machine.sleep(duration)
442-
pwm.deinit()
443-
```
444-
445441
## Math
446442

447443
### abs()
448444

445+
`abs(value)`
446+
447+
Returns the absolute value of a number.
448+
449449
**Example:**
450450

451451
```python
452452
result = abs(-5)
453+
print(result)
453454
```
454455

455456
### constrain()
456457

458+
`constrain(value, min, max)`
459+
460+
Constrains the value to be within a specific range.
461+
457462
**Example:**
458463

459464
```python
460465
def constrain(value, lower, upper):
461466
return max(min(value, upper), lower)
462467

463468
result = constrain(10, 0, 5) # Result will be 5
469+
print(result)
464470
```
465471

466472

467473
### map()
468474

475+
`map(value, in_min, in_max, out_min, out_max)`
476+
477+
Maps a value from one range to another.
478+
469479
**Example:**
470480

471481
```python
472482
def map(value, in_min, in_max, out_min, out_max):
473483
return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
474484

475-
result = map(50, 0, 100, 0, 255)
485+
result = map(50, 0, 100, 0, 255) # Result is 127.5
486+
print(result)
476487
```
477488

478489

479490
### max()
480491

492+
`max(value1, value2, value3, valueX)`
493+
494+
Returns the highest value among the given parameters. Accepts many arguments separated by comma.
495+
481496
**Example:**
482497

483498
```python
484499
result = max(5, 10, 3) # Result will be 10
500+
print(result)
485501
```
486502

487503
### min()
488504

505+
`min(value1, value2, value3, valueX)`
506+
507+
Returns the lowest value among the given parameters. Accepts many arguments separated by comma.
508+
489509
**Example:**
490510

491511
```python
492512
result = min(5, 10, 3) # Result will be 3
513+
print(result)
493514
```
494515

495516
### pow()
496517

518+
`pow(base, exponent)`
519+
520+
Raises a number to the power of another.
521+
497522
**Example:**
498523

499524
```python
500525
result = pow(2, 3) # Result will be 8
526+
print(result)
501527
```
502528

503529
### sq()
504530

531+
`sq(value)`
532+
533+
Returns the square of a number.
534+
505535
**Example:**
506536

507537
```python
508538
result = sq(4) # Result will be 16
539+
print(result)
509540
```
510541

511542
### sqrt()
512543

544+
`math.sqrt(value)`
545+
546+
This function returns the square root of a number.
547+
513548
**Example:**
514549

515550
```python
516551
import math
517552

518553
result = math.sqrt(16) # Result will be 4.0
554+
print(result)
519555
```
520556

521557
## Trigonometry

0 commit comments

Comments
 (0)