EUREKA!!!!
With this setup it works
Wire.begin(26, 32); // (sda, sck)
M5.begin(true, false, true);
Serial.begin(115200);
EUREKA!!!!
With this setup it works
Wire.begin(26, 32); // (sda, sck)
M5.begin(true, false, true);
Serial.begin(115200);
tnks a lot!!!
this one compile & no error in serial monitor
/* Ultrasonic distance sensor example - Better Version
Paul Carpenter, PC Services
26-Jan-2017
Update 4-Feb-2017 PC make variables more obvious
Update 13-May-2017 PC add half bit rounding to distance calculation
Uses HC-SR04 four pin ultrasonic sensor to continuously output distances
found when in range (every second).
Results WHEN valid output onto serial port at 115,200 baud
Both LEDs ON when program starts and any other time for no start of echo
received (probably bad wiring or faulty unit) on start up also serial port
error string
Lights RED Led when No Reading received or out of range
Lights Yellow LED when too close (less than 5 cm)
LEDs are connected via 330R or similar resistor other end of LED to GND
Works on principle sound travels at 343.2 m/s in dry STILL air at 20 deg C
So time per cm is 29.137529 micro seconds /cm
For round trip (there and back) 58.275058 micro seconds /cm
In order to reduce code size and execution time values are kept as integers
and factor of 58 to calculate distances
*/
/* Pin definitions */
#define echopin 21
#define trigpin 22
//#define redled 6
//#define yellowled 7
/* time between readings in ms
On Arduino Mega readings belown 20ms interval could
have residual echo issues */
#define INTERVAL 30
/* Scale factor round trip micro seconds per cm */
#define SCALE_CM 58
#define SCALE_CM_ROUND (SCALE_CM/2)
/* Timeout for distance sensing rather than 1 second in us */
#define MAX_ECHO 300000
#define MIN_ECHO (3 * SCALE_CM)
/* Timeout for start of ECHO pulse being received in us */
#define MAX_START_ECHO 1000
/* Limits for application ranges in cm */
#define MIN_RANGE 4
#define MAX_RANGE 500
#define MAX_ERROR 10
/* calculated distance in centimetres */
unsigned long distance;
unsigned int echotime;
unsigned long next_time, new_time;
/* Distance sensor function to get echo time
Note most ultrasonic distance sensors are only usable beyond 3 cm
So usable range is 3 * SCALE_CM to MAX_ECHO, therefore anything below
3 * SCALE_CM should be treated as error
Returns echo time in microseconds
Maximum MAX_ECHO
Minimum 3 * SCALE_CM (minimum usable)
error codes
Error 2 Echo HIGH before start
Error 1 Echo did not start
Error 0 NO ECHO (Timeout)
Timeout for measurements set by MAX_ECHO
*/
unsigned long GetEchoTime( )
{
unsigned long start_time;
unsigned long end_time;
/* check Echo if high return error */
if( digitalRead( echopin ) )
return( 2 );
/* note start time */
start_time = micros( );
/* send the trigger pulse */
digitalWrite( trigpin, HIGH );
delayMicroseconds( 10 );
digitalWrite( trigpin, LOW );
/* Set timeout for start of echo pulse */
end_time = start_time + MAX_START_ECHO;
/* check ECHO pin goes high within MAX_START_ECHO
if not return error of 1 */
while( !digitalRead( echopin ) )
if( micros( ) > end_time )
return( 1 );
/* Check for Length of echo occurred or timeout */
start_time = micros( );
end_time = start_time + MAX_ECHO;
while( digitalRead( echopin ) )
if( micros( ) > end_time )
break;
end_time = micros( );
/* Return time or timeout */
return( ( start_time < end_time ) ? end_time - start_time: 0 );
}
void setup( )
{
/* set time from reset */
next_time = INTERVAL;
Serial.begin( 115200 );
/* Configure pins and ensure trigger is OFF */
pinMode( trigpin, OUTPUT );
digitalWrite( trigpin, LOW );
pinMode( echopin, INPUT );
/* Configure LED drive and both LEDs On at start up */
//pinMode( redled, OUTPUT );
//pinMode( yellowled, OUTPUT );
//digitalWrite( redled, HIGH );
//digitalWrite( yellowled, HIGH );
/* Send signon message */
Serial.println( F( "PC Services - Better Range test" ) );
/* Do test reading to check if unit connected */
distance = GetEchoTime( );
if( distance > 0 && distance <= 10 )
{
Serial.println( F( "No unit found - Error = " ) );
Serial.println( distance );
}
}
void loop( )
{
new_time = millis( ); /* check if to run this time */
if( new_time >= next_time )
{
/* Turn LEDs Off */
// digitalWrite( redled, LOW );
// digitalWrite( yellowled, LOW );
/* Calculate distance */
echotime = GetEchoTime( );
/* only scale valid readings 0 is timeout or 1 is no echo
realistically minimum accurate or physical range is 3cm */
if( echotime > MAX_ERROR )
{
// Valid number covert to cm
distance = echotime;
distance += SCALE_CM_ROUND; // add in half bit rounding
distance /= SCALE_CM;
}
/* catch errors first */
if( echotime <= MAX_ERROR || distance > MAX_RANGE )
{
// digitalWrite( redled, HIGH ); // Range error too large
if( echotime > 0 && echotime <= MAX_ERROR )
// digitalWrite( yellowled, HIGH ); // Light 2nd LED error
Serial.println("MAX");
}
else
if( distance < MIN_RANGE )
// digitalWrite( yellowled, HIGH ); // Range too close
Serial.println("MIN");
else
Serial.println( int( distance ) ); // In range output distance
next_time = new_time + INTERVAL; // save next time to run
}
}
the first sketch works, with M5Stack basic, if I modify the M5Stack.cpp library from:
// TF Card
if (SDEnable == true) {
SD.begin(TFCARD_CS_PIN, SPI, 40000000);
}
to
// TF Card
if (SDEnable == true) {
SD.begin(TFCARD_CS_PIN, SPI, 10000000);
}
@macsbug said in ATOM Lite Display not display in smallHd 503:
1920/2,1080/2,24
No way to display something on the 503.... I tried all configuration:
// M5AtomDisplay display(); // 1280 x 720
M5AtomDisplay display(1920,1080,24); // 1920 x 1080 , 24bit
// M5AtomDisplay display(1366,768); // 1366 x 768
// M5AtomDisplay display(1920/2,1080/2,24);
// ( 480, 1920);(1920, 480);(1280, 720);(1024, 768);
// ( 960, 540);( 800, 600);( 640, 480);( 640, 400);
// ( 640, 360);( 512, 212);( 256, 192);( 320, 240);
// ( 240, 320);( 200, 200);( 240, 135);( 135, 240);
// ( 160, 160);( 160, 80);( 80, 160);( 80, 80);
when I connect hdmi cable to the 503, the monitor "feel" something (green led on and image settings in the "in settings page" of 503) but no image displayed.....
the same cable works in other setup....
/*
This example shows how to take simple range measurements with the VL53L1X. The
range readings are in units of mm.
*/
#include <Wire.h>
#include <VL53L1X.h>
VL53L1X sensor;
void setup()
{
Serial.begin(115200);
//Wire.begin();
Wire.begin(25, 13); // (SDA, SCL) grove stick
//Wire.begin(21, 22); // (SDA, SCL) grove stack
Wire.setClock(400000); // use 400 kHz I2C
sensor.setTimeout(500);
if (!sensor.init())
{
Serial.println("Failed to detect and initialize sensor!");
while (1);
}
// Use long distance mode and allow up to 50000 us (50 ms) for a measurement.
// You can change these settings to adjust the performance of the sensor, but
// the minimum timing budget is 20 ms for short distance mode and 33 ms for
// medium and long distance modes. See the VL53L1X datasheet for more
// information on range and timing limits.
sensor.setDistanceMode(VL53L1X::Long);
sensor.setMeasurementTimingBudget(50000);
// Start continuous readings at a rate of one measurement every 50 ms (the
// inter-measurement period). This period should be at least as long as the
// timing budget.
sensor.startContinuous(50);
}
void loop()
{
//Serial.print(sensor.read()); //MM
Serial.print(sensor.read()/10); //CM
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
}
it works!!!
from SmallHD503 datasheet:
HDMI Input Types
YCC 4:2:2 @ 8 bits, 8/10/12 bit color in RGB & YCC 4:4:4: 1080p60, 1080p59.94, 1080p50, 1080p30, 1080p29.97 1080p25, 1080p24, 1080p23.98,1080i60, 1080i59.94, 1080i50, 1080i30, 1080i29.97, 1080i25, 720p120, 720p119.88, 720p100, 720p60, 720p59.94 , 720p50, 720p30, 720p29.97, 720p25, 720p24, 720p23.98, 480p120, 480p119.88, 480p60, 480p59.94, 576p100, 576p50, 480i240, 480i239.76, 480i120, 480i119.88, 480i60, 480i59.94, 480i30, 480i29.97, 480i24, 480i23.98, 576i200, 576i100, 576i50, 576i25
EUREKA!!!!
With this setup it works
Wire.begin(26, 32); // (sda, sck)
M5.begin(true, false, true);
Serial.begin(115200);
@flypeek this code, Atom g21 and g25 connected to the scroll grove port and library original pin assigment, works!!!
#include <M5Atom.h>
#include <M5UnitScroll.h>
M5UnitScroll unitScroll;
void setup() {
M5.begin();
Serial.begin(115200);
if (!unitScroll.begin()) {
// if (!unitScroll.begin(&Wire, 0x40, 26, 32, 400000U)) {
// if (!unitScroll.begin(&Wire, 0x40, 32, 26, 400000U)) {
Serial.println("Errore nell'inizializzazione del sensore");
while (1); // Ferma il programma in caso di errore
}
// LED
unitScroll.setLEDColor(0x0000FF);
}
void loop() {
int32_t encoderValue = unitScroll.getEncoderValue();
Serial.print("Valore encoder: ");
Serial.println(encoderValue);
if (unitScroll.getButtonStatus()) {
Serial.println("Pulsante premuto!");
unitScroll.setLEDColor(0xFF0000); // Rosso
delay(500);
unitScroll.setLEDColor(0x00FF00); // Verde
delay(500);
}
delay(100);
}
but I need to connect the unit to the Atom grove port.... some tips??
@flypeek … means I can’t use atom grove port to connect to scroll unit?!?
I tried an i2c scanner (not the atom one that doesn't work):
#include <Wire.h>
void setup()
{
Wire.begin(26, 32);
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
and I can see 0x40
next I tried to put in two tab the modified library M5UnitScroll.h (for scl end sda atom pin) and M5UnitScroll.cpp..
M5UnitScroll.h
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
#ifndef _M5UNITSCROLL_H_
#define _M5UNITSCROLL_H_
#include "Arduino.h"
#include "Wire.h"
#include "pins_arduino.h"
#define SCROLL_ADDR 0x40
#define ENCODER_REG 0x10
#define BUTTON_REG 0x20
#define RGB_LED_REG 0x30
#define RESET_REG 0x40
#define INC_ENCODER_REG 0x50
#define BOOTLOADER_VERSION_REG 0xFC
#define JUMP_TO_BOOTLOADER_REG 0xFD
#define FIRMWARE_VERSION_REG 0xFE
#define I2C_ADDRESS_REG 0xFF
class M5UnitScroll {
private:
uint8_t _addr;
TwoWire* _wire;
uint8_t _scl;
uint8_t _sda;
uint32_t _speed;
void writeBytes(uint8_t addr, uint8_t reg, uint8_t* buffer, uint8_t length);
void readBytes(uint8_t addr, uint8_t reg, uint8_t* buffer, uint8_t length);
public:
bool begin(TwoWire* wire = &Wire, uint8_t addr = SCROLL_ADDR, uint8_t sda = 26, uint8_t scl = 32, uint32_t speed = 400000U);
int32_t getEncoderValue(void);
int32_t getIncEncoderValue(void);
bool getButtonStatus(void);
void setLEDColor(uint32_t color, uint8_t index = 0);
uint32_t getLEDColor(void);
void setEncoderValue(int32_t encoder);
void resetEncoder(void);
bool getDevStatus(void);
uint8_t getBootloaderVersion(void);
uint8_t getFirmwareVersion(void);
uint8_t setI2CAddress(uint8_t addr);
uint8_t getI2CAddress(void);
void jumpBootloader(void);
};
#endif
and M5UnitScroll.cpp
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
#include "M5UnitScroll.h"
/*! @brief Initialize the Encoder. */
bool M5UnitScroll::begin(TwoWire *wire, uint8_t addr, uint8_t sda, uint8_t scl, uint32_t speed) {
_wire = wire;
_addr = addr;
_sda = sda;
_scl = scl;
_speed = speed;
_wire->begin(_sda, _scl);
_wire->setClock(_speed);
delay(10);
_wire->beginTransmission(_addr);
uint8_t error = _wire->endTransmission();
if (error == 0) {
return true;
} else {
return false;
}
}
/*! @brief Write a certain length of data to the specified register address. */
void M5UnitScroll::writeBytes(uint8_t addr, uint8_t reg, uint8_t *buffer, uint8_t length) {
_wire->beginTransmission(addr);
_wire->write(reg);
for (int i = 0; i < length; i++) {
_wire->write(*(buffer + i));
}
_wire->endTransmission();
}
/*! @brief Read a certain length of data to the specified register address. */
void M5UnitScroll::readBytes(uint8_t addr, uint8_t reg, uint8_t *buffer, uint8_t length) {
uint8_t index = 0;
_wire->beginTransmission(addr);
_wire->write(reg);
_wire->endTransmission(false);
_wire->requestFrom(addr, length);
for (int i = 0; i < length; i++) {
buffer[index++] = _wire->read();
}
}
/*! @brief Read the encoder value.
@return The value of the encoder that was read */
int32_t M5UnitScroll::getEncoderValue(void) {
int32_t value = 0;
readBytes(_addr, ENCODER_REG, (uint8_t *)&value, 4);
return value;
}
/*! @brief Read the encoder inc value.
@return The value of the encoder that was read */
int32_t M5UnitScroll::getIncEncoderValue(void) {
int32_t value = 0;
readBytes(_addr, INC_ENCODER_REG, (uint8_t *)&value, 4);
return value;
}
/*! @brief Get the current status of the rotary encoder button.
@return true if the button was pressed, otherwise false. */
bool M5UnitScroll::getButtonStatus(void) {
uint8_t data;
readBytes(_addr, BUTTON_REG, &data, 1);
return data == 0x00;
}
/*! @brief Set the color of the LED (HEX). */
void M5UnitScroll::setLEDColor(uint32_t color, uint8_t index) {
uint8_t data[4];
data[3] = color & 0xff;
data[2] = (color >> 8) & 0xff;
data[1] = (color >> 16) & 0xff;
data[0] = index;
writeBytes(_addr, RGB_LED_REG, data, 4);
}
/*! @brief Get the color of the LED (HEX).
@return The value of the led that was read */
uint32_t M5UnitScroll::getLEDColor(void) {
uint8_t data[4];
uint32_t value = 0;
readBytes(_addr, RGB_LED_REG, data, 4);
value = (data[3] | (data[2] << 8) | (data[1] << 16));
return value;
}
void M5UnitScroll::setEncoderValue(int32_t encoder) {
writeBytes(_addr, ENCODER_REG, (uint8_t *)&encoder, 4);
}
void M5UnitScroll::resetEncoder(void) {
uint8_t data = 1;
writeBytes(_addr, 0x40, &data, 1);
}
/*! @brief Get the dev status.
@return 1 if the dev working, otherwise 0.. */
bool M5UnitScroll::getDevStatus(void) {
_wire->beginTransmission(_addr);
if (_wire->endTransmission() == 0)
return true;
else
return false;
}
uint8_t M5UnitScroll::getBootloaderVersion(void) {
_wire->beginTransmission(_addr);
_wire->write(BOOTLOADER_VERSION_REG);
_wire->endTransmission(false);
uint8_t RegValue;
_wire->requestFrom(_addr, 1);
RegValue = _wire->read();
return RegValue;
}
uint8_t M5UnitScroll::getFirmwareVersion(void) {
_wire->beginTransmission(_addr);
_wire->write(FIRMWARE_VERSION_REG);
_wire->endTransmission(false);
uint8_t RegValue;
_wire->requestFrom(_addr, 1);
RegValue = _wire->read();
return RegValue;
}
uint8_t M5UnitScroll::setI2CAddress(uint8_t addr) {
uint8_t temp[2] = {0};
temp[0] = I2C_ADDRESS_REG;
_wire->beginTransmission(_addr);
_wire->write(temp[0]);
_wire->write(addr);
_wire->endTransmission();
_addr = addr;
return _addr;
}
uint8_t M5UnitScroll::getI2CAddress(void) {
uint8_t temp[2] = {0};
temp[0] = I2C_ADDRESS_REG;
_wire->beginTransmission(_addr);
_wire->write(temp[0]);
_wire->endTransmission(false);
uint8_t RegValue;
_wire->requestFrom(_addr, 1);
RegValue = _wire->read();
return RegValue;
}
void M5UnitScroll::jumpBootloader(void) {
uint8_t value = 1;
writeBytes(_addr, JUMP_TO_BOOTLOADER_REG, (uint8_t *)&value, 1);
}
but this code outputs "error"
#include <M5Atom.h>
#include <Wire.h>
#include "M5UnitScroll.h"
// #include <M5UnitScroll.h>
M5UnitScroll unitScroll;
void setup() {
M5.begin();
Serial.begin(115200);
Wire.begin(26, 32);
if (!unitScroll.begin()) {
Serial.println("Error");
while (1);
}
unitScroll.setLEDColor(0x0000FF);
}
void loop() {
int32_t encoderValue = unitScroll.getEncoderValue();
Serial.print("Valore encoder: ");
Serial.println(encoderValue);
if (unitScroll.getButtonStatus()) {
Serial.println("Pulsante premuto!");
unitScroll.setLEDColor(0xFF0000); // Red
delay(500);
unitScroll.setLEDColor(0x00FF00); // Green
delay(500);
}
delay(100);
}
@HappyUser I'm not sure how to check the begin function return value..
I tried:
#include <M5Atom.h>
#include "M5UnitScroll.h"
M5UnitScroll Scroll;
bool scrolla;
void setup() {
M5.begin();
Serial.begin(115200);
Scroll.begin();
}
void loop() {
scrolla = Scroll.begin();
Serial.println(scrolla);
delay(1000);
scrolla = Scroll.begin(&Wire, SCROLL_ADDR, 32, 26);
Serial.println(scrolla);
delay(1000);
scrolla = Scroll.begin(&Wire, SCROLL_ADDR, 26, 32);
Serial.println(scrolla);
delay(1000);
}
and the output in the serial monitor is:
10:56:20.024 -> 0
10:56:21.044 -> 0
10:56:22.062 -> 0
10:56:23.084 -> 0
10:56:24.071 -> 0
10:56:25.092 -> 0
10:56:26.081 -> 0
10:56:27.102 -> 0
10:56:28.129 -> 0
this code doesn't work
#include <M5Atom.h>
#include <M5UnitScroll.h>
M5UnitScroll Scroll;
int myVariable = 0;
int previousEncoderValue = 0;
bool buttonPressed = false;
void setup() {
M5.begin();
Serial.begin(115200);
Scroll.begin(&Wire, 0x40, 26, 32);
}
void loop() {
int currentEncoderValue = Scroll.getEncoderValue();
int difference = currentEncoderValue - previousEncoderValue;
myVariable += difference;
previousEncoderValue = currentEncoderValue;
if (Scroll.getButtonStatus()) {
if (!buttonPressed) {
myVariable = 0;
buttonPressed = true;
}
} else {
buttonPressed = false;
}
Serial.print("myVariable ");
Serial.println(myVariable);
Serial.print("buttonPressed ");
Serial.println(buttonPressed);
}
some tips?
Hi All, I can't find an Arduino example about Unit Scroll.. I would like to implement selection and click...M5Unit-Scroll
best regards
@AreaKode on GitHub I found only the .bin file....
is there an Arduino version of the code? I would like to add some functions..
@AreaKode .... also putting a small peace of paper in between joy and atom works to me!! maybe a more portable solution!!