# Programming with MicroPython: Sugar and Powerbrick Modules

### Import Sugar Library

Import the Sugar library to make use of its functions.

```
from sugar import *
```

### 14: Sugar and Powerbrick modules

### PIR Sensor

[PIR Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_pir.html)

```
# available pins
# 'P0','P1','P2','P3','P8','P9','P12','P13','P14','P15','P16'

value=PIR(pin).value()
```

Returns a boolean value from the PIR sensor.

* pin: The pin connected to the sensor.

### IR Line Tracker

[IR Line Tracker Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_line.html)

```
# available pins
# 'P0','P1','P2','P3','P8','P9','P12','P13','P14','P15','P16'

value=Tracker(pin).value()
```

Returns a boolean value from the Line Tracker.

* pin: The pin connected to the sensor.

### (Hall Effect)Magnet Sensor

[Magnet Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_magnet.html)

```
# available pins
# 'P0','P1','P2','P3','P8','P9','P12','P13','P14','P15','P16'

value=Hall(pin).value()
```

Returns a boolean value from the Magnet sensor.

* pin: The pin connected to the sensor.

### Button Sensor

[Button Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_button.html)

```
# available pins
# 'P0','P1','P2','P3','P8','P9','P12','P13','P14','P15','P16'

value=Button(pin).value()
```

Returns a boolean value from the Button sensor.

* pin: The pin connected to the sensor.

### LED Module

[LED Module Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_led.html)

```
# available pins
# 'P0','P1','P2','P3','P8','P13','P14','P15','P16'
# available states
# 'ON','OFF'

LED(pin).state(state) #ON/OFF
```

Sets the state of the LED module to be on or off.

```
LED(pin).brightness(brightness) #Brightness
```

Sets the LED brightness.

* pin: The pin connected to the sensor.
* state: on/off.
* brightness: 0\~100.

### Flame Sensor

[Flame Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_fire.html)

```
# available pins
# 'P0','P1','P2','P3','P12','P14','P15','P16'

value=Flame(pin).value()
```

Returns an analog value from the Flame sensor.

* pin: The pin connected to the sensor.

### Potentiometer Sensor

[Potentiometer Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_potentialmeter.html)

```
# available pins
# 'P0','P1','P2','P3','P12','P14','P15','P16'

value=Angle(pin).value()
```

Returns an analog value from the Potentiometer sensor.

* pin: The pin connected to the sensor.

### Light Sensor

[Light Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_light.html)

```
# available pins
# 'P0','P1','P2','P3','P12','P14','P15','P16'

value=Light(pin).value()
```

Returns an analog value from the Light sensor.

* pin: The pin connected to the sensor.

### Soil Moisture Sensor

[Soil Moisture Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_soil.html)

```
# available pins
# 'P0','P1','P2','P3','P12','P14','P15','P16'

value=Soil(pin).value()
```

Returns an analog value from the Soil Moisture sensor.

* pin: The pin connected to the sensor.

### Water Sensor

[Water Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_water.html)

```
# available pins
# 'P0','P1','P2','P3','P12','P14','P15','P16'

value=WaterLevel(pin).value()
```

Returns an analog value from the Water sensor.

* pin: The pin connected to the sensor.

### Environment Sensor(Sugar)

[Environment Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_env.html)

```
x=ENV().update()

temperature=x[0]
humidity=x[1]
```

Returns a list containing the temperature and humidity readings from the Environment sensor.

### TOF Laser Distance Module

[TOF Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_tof.html)

```
value=TOFDistance().value()
```

Returns the distance in mm from the laser distance module.

### Joystick Module

[Joystick Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/sugar/sugar_joystick.html)

```
# available positions
# 'up', 'down', 'left', 'right', 'pressed'

position=Joystick().state() #Joystick Position
x_value=Joystick().value('x') #Joystick X value
y_value=Joystick().value('y') #Joystick Y value
```

Returns the value from the Joystick module.

### RTC Clock Module

[RTC CLock Module Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/kittenClock)

```
Clock().setTime((year,month,day,weekday,hour,min,sec))
```

Sets the time on the Clock module.

```
# available states
# 'pause','open'

Clock().modeSet(state=mode)
```

Starts or stops the clock.

```
Clock().refreshTime(8)
```

Syncs the clock with UTC time.(Needs network connection)

```
# available fields
# 'all','year','month','day','week','hour','minute','second'

value=Clock().getTime(field)
```

Returns a value from the clock module.

### Ultrasound Sensor

[Ultrasound Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/nekoUltrasound.html)

```
# available pins
# 'P0','P1','P2','P3','P8','P9','P12','P13','P14','P15','P16'

value=MeowSonar(pin).ping()
```

Returns the distance measured by the ultrasound sensor.

* pin: The pin connected to the sensor.

### RFID Module(New)

[Sugar RFID Module Information](/functional_module/sugar/sugar-rfid-module.md)

```python
from rfid import *
```

Imports the RFID library.

```
rfids = RFID()
```

Initiates the RFID Object.

```
rfids.scanCar()
```

Returns the UUID. Returns None if no card is present.

```
rfids.writeBlock(block,data)
```

Writes data onto the RFID tag.

* block: RFID Block, 0\~46
* data: Data to be written(string)

```
rfids.readBlock(block)
```

Reads the data from the RFID Block.

* block: RFID Block, 0\~46

### RFID Module(Old)

[RFID Module Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/PWmodules/powerBrickRFID.html)

```
RFID().probe() #without callback
RFID().probe(probeCallback if 'probeCallback' in dir() else None) #with callback
```

Starts the scan for RFID tag.

```
RFID().stop()
```

Stops the RFID scanning.

```
RFID().uuid()
```

Returns the RFID UUID.

```
RFID().write(SECTOR, BLOCK, DATA)
RFID().read(SECTOR, BLOCK)
```

Writes or read the data from the RFID tag.

* SECTOR: RFID sector, 1\~16.
* BLOCK: RFID block, 0\~2.
* DATA: The data to be written.

### Colour and Gesture Module

[Colour and Gesture Module Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/PWmodules/powerBrickColorGesture.html)

```
# available type
# 1,2,3,4

ColorGes().mode(type)
```

Sets the mode for the Colour and Gesture Module.

* type: mode

  * Colour Mode: 1
  * Distance Mode: 2
  * Gesture Mode: 3
  * LED Mode: 4

  hue = ColorGes().read(0) brightness = ColorGes().read(1)

Returns the colour values, hue and brightness, when mode is set to colour.

```
distance = ColorGes().distance()
```

Returns the distance when mode is set to distance. Value ranges from 0 to 255.

```
gesture = ColorGes().gesture()
```

Returns the last gesture when mode is set to gesture.

```
ColorGes().ledpwm(0) #LED Brightness
ColorGes().led((0,0,0,0)) #LED State
```

Sets the LED brightness and state when mode is set to LED.

* LED Brightness: 0\~100
* LED State: 0 for off, 1 for on

### MP3 Module

[MP3 Module Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/PWmodules/powerBrickMP3.html)

```
# available commands
# MP3().PLAY, MP3().STOP, MP3().NEXT, MP3().PREV

MP3().operate(op)
```

Sends the command to the MP3 module.

* op: Command to send

  * MP3 play: MP3().PLAY
  * MP3 stop: MP3().STOP
  * MP3 next song: MP3().NEXT
  * MP3 previous song: MP3().PREV

  MP3().vol(volume)

Sets the volume for the MP3 player. (0\~30)

```
MP3().playIndex(index) 
```

Specify the song to play by index. (The index on the SD card)

```
MP3().playName(name) 
```

Specify the song to play by file name. (Cannot be more than 8 characters)

### Environment Module(Powerbrick)

[Environment Module Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/PWmodules/powerBrickEnvironment.html)

```
# available pin
# 'P0','P1','P2','P3','P4','P5','P6','P7','P8','P9','P10','P11','P12','P13','P14','P15','P16'

dht11 = DHT11(pin)
```

Creates a DHT11 object.

* pin: The pin connected to the sensor.

  dht11.measure()

Asks the sensor to measure environment data.

```
temperature = dht11.temperature()
humidity = dht11.humidity()
```

Returns the temperature and humidity readings from the sensor.

### Water-proof Temperature Sensor

[Water-proof Temperature Sensor Information](https://kittenbothk-eng.readthedocs.io/en/latest/functional_module/watertemp.html)

```
# available pins
# 'P0','P1','P2','P3','P8','P9','P12','P13','P14','P15','P16'

value=DS18B20(pin).read()
```

Reurns the temperature reading from the sensor.

* pin: The pin connected to the sensor.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sharinghub-eng.kittenbot.hk/main-controllers/futureboard/futureboard-micropython-programming-tutorial/programming-with-micropython-sugar-and-powerbrick-modules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
